package app.closer.ui.pairing

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import app.closer.R
import app.closer.core.navigation.AppRoute
import app.closer.ui.auth.AuthBackgroundBrush
import app.closer.ui.auth.AuthInk
import app.closer.ui.auth.AuthMuted
import app.closer.ui.auth.AuthOnPrimary
import app.closer.ui.auth.AuthPrimary
import app.closer.ui.auth.AuthPrimaryDeep
import app.closer.ui.theme.CloserPalette

private val WHY_ITEMS = listOf(
    "Daily questions reveal together — both answer privately, then see each other's response at the same time.",
    "Couples games: This or That, How Well Do You Know Me, Desire Sync, and more.",
    "A shared timeline of answers, moments, and milestones you build together over time."
)

@Composable
fun PairPromptScreen(
    onNavigate: (String) -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AuthBackgroundBrush)
            .safeDrawingPadding()
            .navigationBarsPadding()
            .verticalScroll(rememberScrollState())
            .padding(horizontal = 28.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(Modifier.height(56.dp))

        Surface(
            shape = RoundedCornerShape(20.dp),
            color = AuthPrimary.copy(alpha = 0.12f)
        ) {
            Text(
                text = "You're in",
                modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
                style = MaterialTheme.typography.labelLarge,
                color = AuthPrimaryDeep,
                fontWeight = FontWeight.SemiBold
            )
        }

        Spacer(Modifier.height(20.dp))

        Image(
            painter = painterResource(R.drawable.illustration_couple_invite),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(width = 176.dp, height = 230.dp)
                .clip(RoundedCornerShape(28.dp))
        )

        Spacer(Modifier.height(20.dp))

        Text(
            text = "Now bring\nyour person in.",
            style = MaterialTheme.typography.displaySmall.copy(fontWeight = FontWeight.Bold),
            color = AuthInk,
            textAlign = TextAlign.Center
        )

        Spacer(Modifier.height(16.dp))

        Text(
            text = "Closer is built for two. Connect your partner to unlock everything worth unlocking:",
            style = MaterialTheme.typography.bodyLarge,
            color = AuthMuted,
            textAlign = TextAlign.Center
        )

        Spacer(Modifier.height(28.dp))

        Column(
            modifier = Modifier.fillMaxWidth(),
            verticalArrangement = Arrangement.spacedBy(14.dp)
        ) {
            WHY_ITEMS.forEach { item ->
                WhyRow(text = item)
            }
        }

        Spacer(Modifier.height(40.dp))

        Button(
            onClick = { onNavigate(AppRoute.CREATE_INVITE) },
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp),
            shape = RoundedCornerShape(16.dp),
            colors = ButtonDefaults.buttonColors(
                containerColor = AuthPrimary,
                contentColor = AuthOnPrimary
            )
        ) {
            Text("Invite my partner", style = MaterialTheme.typography.labelLarge)
        }

        Spacer(Modifier.height(12.dp))

        TextButton(
            onClick = { onNavigate(AppRoute.HOME) },
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                "I'll do this later",
                style = MaterialTheme.typography.bodyMedium,
                color = AuthMuted
            )
        }

        Spacer(Modifier.height(32.dp))
    }
}

@Composable
private fun WhyRow(text: String) {
    Row(
        horizontalArrangement = Arrangement.spacedBy(12.dp),
        verticalAlignment = Alignment.Top
    ) {
        Surface(
            shape = RoundedCornerShape(8.dp),
            color = CloserPalette.PurpleDeep,
            modifier = Modifier.size(22.dp)
        ) {
            Icon(
                imageVector = Icons.Filled.Check,
                contentDescription = null,
                tint = AuthOnPrimary,
                modifier = Modifier
                    .padding(4.dp)
                    .size(14.dp)
            )
        }
        Text(
            text = text,
            style = MaterialTheme.typography.bodyMedium,
            color = AuthInk.copy(alpha = 0.82f)
        )
    }
}
