import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { ArticleContent, PreambleContent, LEGAL_STATUS_LABELS, } from "@/lib/acts/types"; import { getActById, getActTypeById, getInstitutionById, getActVersions, getCurrentActVersion, getActBlocks, getActApprovals, } from "@/lib/acts/repository"; import { getPositions, getCitizenshipPositions } from "@/lib/positions/repository"; import { getActiveCitizenship } from "@/lib/citizenship/repository"; import { getActFlow, getNextPhase } from "@/lib/acts/flows"; import { ApproveButton } from "./approve-button"; import { VetoButton } from "./veto-button"; import { getCurrentUser, getUsers } from "@/lib/user"; import { cn, formatDate } from "@/lib/utils"; import { Metadata } from "next"; import { Merriweather } from "next/font/google"; import Image from "next/image"; import Link from "next/link"; import { notFound } from "next/navigation"; const documentText = Merriweather({ weight: "400" }); const documentHeading = Merriweather({ weight: "700" }); export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise { const { id } = await params; const act = await getActById(id); if (!act) return {}; const type = await getActTypeById(act.actTypeId); return { title: `${type?.name ?? ""}${act.title ? ` “${act.title}”` : ""}`, }; } export default async function Act({ params }: { params: Promise<{ id: string }> }) { const { id } = await params; const user = await getCurrentUser(); const act = await getActById(id); if (!act) notFound(); const type = await getActTypeById(act.actTypeId); const institution = await getInstitutionById(act.issuingInstitutionId); const [author] = await getUsers(act.authorId); const versions = await getActVersions(id); const version = await getCurrentActVersion(id); if (!version) notFound(); const blocks = await getActBlocks(version.id); const needsRegion = institution?.kind === "REGION"; const preamble = blocks.find((b) => b.type === "PREAMBLE"); const preambleContent = preamble?.content as PreambleContent | undefined; const approvals = await getActApprovals(version.id); const positions = await getPositions(); const positionNames = new Map(positions.map((p) => [p.id, p.name])); const flow = getActFlow(type?.code ?? ""); const phase = getNextPhase(flow, approvals); const stageLabel = phase?.label ?? "Completed"; let canApprove = false; let canVeto = false; if (user && act.legalStatus === "DRAFT" && phase) { const citizenship = await getActiveCitizenship(user.id); if (citizenship) { const citizenshipPositions = await getCitizenshipPositions(citizenship.id); const holds = citizenshipPositions.some( (p) => !p.resignedAt && p.position.capabilities.some((c) => c.capability === phase.capability) ); canApprove = holds; canVeto = holds && !!phase.allowVeto; } } return (

{type?.name} {act.title && `“${act.title}”`}{" "} {act.numberInSeries ? `№${act.numberInSeries}/${act.numberYear}` : without a number}

Issuing institution

{institution?.name}

Author

{author?.firstName} {author?.secondName}

Created

{formatDate(act.createdAt)}

Version from

{formatDate(version.createdAt)}

Process stage

{stageLabel}

Legal status

{act.legalStatus === "NOT_ADOPTED" ? {LEGAL_STATUS_LABELS[act.legalStatus]} :

{LEGAL_STATUS_LABELS[act.legalStatus]}

}

History

{versions.map((v) => v.id === version.id ? (

Version from {formatDate(v.createdAt)}

Current
) : (

Version from {formatDate(v.createdAt)}

Outdated ) )}
{ (canApprove || canVeto) && (

Signature

You are able to sign or reject this act

{canApprove && } {canVeto && }
)}
Emblem of the European Federation

{!needsRegion ? "European Federation" : "Region"}

{institution?.name}

{type?.name} {act.numberInSeries && `№${act.numberInSeries}/${act.numberYear}`} {act.title && `“${act.title}”`}

{preambleContent && (

{preambleContent.text}

)} {blocks .filter((b) => b.type === "ARTICLE") .map((block, index) => { const article = block.content as ArticleContent; return (

Article {index + 1}{article.title && `. ${article.title}.`}

{article.intro &&

{article.intro}

}
{article.clauses.map((clause, clauseIndex) => (

{article.markerStyle === "NUMBERS" && `${clauseIndex + 1}. `} {article.markerStyle === "LETTERS" && `${String.fromCharCode(97 + clauseIndex)}) `} {clause.kind === "APPOINTMENT" ? `${clause.action === "ASSIGN" ? "Appoint" : "Dismiss"} ${clause.citizenshipName} ${clause.action === "ASSIGN" ? "to" : "from"} ${positionNames.get(clause.positionId) ?? "an unknown position"}.` : clause.text}

))}
); })}
{approvals.map((a) => (

{a.name}

{a.role}

{a.decision === "APPROVE" ? "Approved" : "Vetoed"} at {formatDate(a.decidedAt)}

))}
); }