package com.cottondb.android.ui.briefing import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.material3.HorizontalDivider 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.alpha import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import androidx.compose.ui.semantics.selected import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.cottondb.android.R import com.cottondb.android.briefing.BriefingBlock import com.cottondb.android.briefing.BriefingBlocks import com.cottondb.android.briefing.BriefingProject import com.cottondb.android.briefing.BriefingRead import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonMetrics import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import com.cottondb.android.home.HomeProjectColour import com.cottondb.android.home.projectColours import com.cottondb.android.ui.paper.MemoryPaperDye import com.cottondb.android.ui.paper.PaperEdge import com.cottondb.android.ui.paper.TornPaper /** A controlled, read-only rendering of the native `brief` result. */ @Composable fun BriefingScreen( read: BriefingRead?, lockedProject: String?, currentProject: String?, loading: Boolean, readProblem: String?, onRetry: () -> Unit, onSelectProject: (String) -> Unit, onBack: () -> Unit, modifier: Modifier = Modifier, ) { Column( modifier = modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding(), ) { BriefingHeader(read, lockedProject, onBack) when { readProblem != null && read == null -> BriefingFailure(readProblem, onRetry) read == null -> BriefingSkeleton() else -> { if (readProblem != null) BriefingCachedFailure(readProblem, onRetry) BriefingContent(read, lockedProject, currentProject, onSelectProject) } } } } @Composable private fun BriefingHeader(read: BriefingRead?, lockedProject: String?, onBack: () -> Unit) { val lockedName = lockedProject?.let { slug -> read?.projects?.firstOrNull { it.slug == slug }?.name ?: slug } Row( Modifier.fillMaxWidth().padding(horizontal = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Column(Modifier.padding(top = 12.dp)) { Text( "Briefing", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink, ) lockedName?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } } } @Composable private fun BriefingFailure(problem: String, onRetry: () -> Unit) { Column(Modifier.padding(CottonMetrics.contentMargin)) { Text( "The briefing could not be read.", style = CottonTextStyles.body, color = CottonPalette.statusError, ) Text(problem, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) TextButton(onClick = onRetry, modifier = Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } } } @Composable private fun BriefingCachedFailure(problem: String, onRetry: () -> Unit) { Row(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin)) { Text( problem, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError, ) TextButton(onClick = onRetry, modifier = Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } } } @Composable private fun BriefingContent( read: BriefingRead, lockedProject: String?, currentProject: String?, onSelectProject: (String) -> Unit, ) { val effectiveProject = effectiveProject(read, lockedProject, currentProject) LazyColumn( contentPadding = PaddingValues(CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(14.dp), ) { if (lockedProject == null) { item { ProjectRail(read.projects, effectiveProject, onSelectProject) } } item { BriefingMeter(read) } item { TornPaper( id = "briefing-${effectiveProject ?: "all"}", dye = MemoryPaperDye.Cream, modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Briefing sheet" }, edge = PaperEdge.Cut, ) { SelectionContainer { Column( Modifier.fillMaxWidth() .heightIn(min = 420.dp) .padding(horizontal = 22.dp, vertical = 30.dp), ) { BriefingBlocks.parse(read.briefing.orEmpty()).forEachIndexed { index, block -> BriefingBlockText(block, first = index == 0) } } } } } } } private fun effectiveProject( read: BriefingRead, lockedProject: String?, currentProject: String?, ): String? = lockedProject ?: currentProject ?: read.briefedProjects.firstOrNull() ?: read.projects.firstOrNull()?.slug @Composable private fun ProjectRail( projects: List, currentProject: String?, onSelectProject: (String) -> Unit, ) { if (projects.isEmpty()) return val colours = projectColours(projects.map(BriefingProject::slug)) Row( modifier = Modifier.horizontalScroll(rememberScrollState()).padding(vertical = 6.dp), horizontalArrangement = Arrangement.spacedBy(16.dp), ) { projects.forEach { project -> val colour = colours.getValue(project.slug) val isSelected = project.slug == currentProject Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.width(72.dp) .clickable(role = Role.Button, onClick = { onSelectProject(project.slug) }) .semantics { contentDescription = "Briefing project ${project.name}"; selected = isSelected }, ) { Box(contentAlignment = Alignment.Center) { Image( painter = painterResource(briefingCapResource(colour)), contentDescription = null, modifier = Modifier.size(72.dp), contentScale = ContentScale.Fit, ) Text( project.name, Modifier.width(58.dp), style = CottonTextStyles.preview.copy( fontFamily = CottonFonts.cap, fontSize = 15.sp, lineHeight = 20.sp, ), color = if (briefingCapHasDarkInk(colour)) CottonPalette.ink else CottonPalette.ground, textAlign = TextAlign.Center, maxLines = 3, overflow = TextOverflow.Ellipsis, ) } Text( project.name, style = CottonTextStyles.caption.copy(fontSize = 12.sp, lineHeight = 15.sp), color = if (isSelected) CottonPalette.ink else CottonPalette.inkSecondary, maxLines = 1, overflow = TextOverflow.Ellipsis, ) HorizontalDivider( Modifier.width(40.dp).padding(top = 2.dp).alpha(if (isSelected) 1f else 0f), thickness = 1.2.dp, color = CottonPalette.ink, ) } } } } @Composable private fun BriefingMeter(read: BriefingRead) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.End) { Text( BriefingBlocks.meter(read.tokensEstimate, read.budgetTokens, read.overBudget), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 16.sp), color = if (read.overBudget) CottonPalette.statusError else CottonPalette.inkSecondary, ) BriefingBlocks.machine(read.machineScoped)?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } } @Composable private fun BriefingBlockText(block: BriefingBlock, first: Boolean) { val gap = BriefingBlocks.gapBefore(block, first).dp when (block) { is BriefingBlock.Heading -> Text( block.text, Modifier.padding(top = gap), style = CottonTextStyles.screenTitle.copy( fontFamily = CottonFonts.serif, fontSize = 22.sp, lineHeight = 27.sp, ), color = CottonPalette.ink, ) is BriefingBlock.Section -> Text( block.text, Modifier.padding(top = gap), style = CottonTextStyles.rowTitle.copy( fontFamily = CottonFonts.hand, fontSize = 19.sp, lineHeight = 23.sp, ), color = CottonPalette.ink, ) is BriefingBlock.Item -> Row( Modifier.padding(top = gap), verticalAlignment = Alignment.Top, ) { Text( "•", Modifier.padding(end = 8.dp), style = briefingBodyStyle(), color = CottonPalette.inkSecondary, ) Text( block.text, Modifier.weight(1f), style = briefingBodyStyle(), color = CottonPalette.ink, ) } is BriefingBlock.Line -> Text( block.text, Modifier.padding(top = gap), style = briefingBodyStyle(), color = CottonPalette.ink, ) BriefingBlock.Rule -> HorizontalDivider( Modifier.fillMaxWidth() .padding(top = gap) .height(3.dp) .semantics { contentDescription = "Briefing rule" }, thickness = 0.8.dp, color = CottonPalette.inkSecondary, ) } } private fun briefingBodyStyle() = CottonTextStyles.caption.copy( fontFamily = CottonFonts.type, fontSize = 13.sp, lineHeight = 17.sp, ) @Composable private fun BriefingSkeleton() { TornPaper( id = "briefing-skeleton", dye = MemoryPaperDye.Cream, modifier = Modifier.fillMaxWidth() .padding(CottonMetrics.contentMargin) .height(420.dp) .alpha(.5f), edge = PaperEdge.Cut, ) {} } private fun briefingCapHasDarkInk(colour: HomeProjectColour): Boolean = colour in setOf( HomeProjectColour.CREAM, HomeProjectColour.YELLOW, HomeProjectColour.SILVER, HomeProjectColour.SKY, HomeProjectColour.PINK, ) private fun briefingCapResource(colour: HomeProjectColour): Int = when (colour) { HomeProjectColour.CREAM -> R.drawable.cap_cream HomeProjectColour.RED -> R.drawable.cap_red HomeProjectColour.NAVY -> R.drawable.cap_navy HomeProjectColour.YELLOW -> R.drawable.cap_yellow HomeProjectColour.GREEN -> R.drawable.cap_green HomeProjectColour.SILVER -> R.drawable.cap_silver HomeProjectColour.ORANGE -> R.drawable.cap_orange HomeProjectColour.BURGUNDY -> R.drawable.cap_burgundy HomeProjectColour.SKY -> R.drawable.cap_sky HomeProjectColour.PINK -> R.drawable.cap_pink HomeProjectColour.BLACK -> R.drawable.cap_black HomeProjectColour.TEAL -> R.drawable.cap_teal }