import { Head, Link, usePage } from '@inertiajs/react';
import PublicLayout from '@/Layouts/PublicLayout';
import Card from '@/Components/UI/Card';
import Button from '@/Components/UI/Button';
import { PageProps } from '@/types';

interface ProgrammeShow {
    id: number;
    title: string;
    slug: string;
    overview: string | null;
    learning_outcomes: string[];
    fee: string | null;
    currency: string;
    duration_months: number | null;
    delivery_mode: string;
    institute: { id: number; name: string; slug: string };
    courses: { id: number; code: string | null; title: string; description: string | null; slug: string }[];
    assessment_types: string[];
    recommended_programmes: { title: string; slug: string; institute_name: string }[];
}

const assessmentCopy: Record<string, { label: string; description: string }> = {
    written_project: {
        label: 'Written Project',
        description: 'A rigorous strategic report analysing personal and organisational influence channels.',
    },
    video_project: {
        label: 'Video Project',
        description: 'A high-production recorded presentation delivering a transformative organisational initiative.',
    },
    practical_application: {
        label: 'Practical Application',
        description: 'A simulated real-world scenario evaluated by senior facilitators and assessors.',
    },
    capstone: {
        label: 'Capstone',
        description: 'Final integration project synthesising all course learning into a viable leadership roadmap.',
    },
};

export default function Show({ programme, isEnrolled }: { programme: ProgrammeShow; isEnrolled: boolean }) {
    const { auth } = usePage<PageProps>().props;

    return (
        <PublicLayout>
            <Head title={programme.title} />

            <section className="mx-auto max-w-5xl px-6 pb-10 pt-16 lg:px-margin-page">
                <p className="text-label-caps text-accent-gold">{programme.institute.name}</p>
                <h1 className="text-institutional-header mt-4 text-on-surface">{programme.title}</h1>
                <p className="text-body-lg mt-4 max-w-2xl text-on-surface-variant">{programme.overview}</p>

                <div className="mt-8 flex flex-wrap gap-4">
                    <div className="rounded-lg bg-jgcp-navy px-6 py-4 text-white">
                        <p className="text-label-caps text-white/60">Programme Fee</p>
                        <p className="text-xl font-semibold">
                            {programme.fee ? `${programme.currency} ${Number(programme.fee).toLocaleString()}` : 'Contact Admissions'}
                        </p>
                        <p className="text-xs text-white/50">Flexible payment plans available</p>
                    </div>
                    <div className="rounded-lg border border-outline-variant px-6 py-4">
                        <p className="text-label-caps text-on-surface-variant">Duration</p>
                        <p className="text-xl font-semibold text-on-surface">
                            {programme.duration_months ? `${programme.duration_months} Months` : '—'}
                        </p>
                        <p className="text-xs capitalize text-on-surface-variant">
                            {programme.delivery_mode.replace('_', ' ')} Format
                        </p>
                    </div>
                </div>
            </section>

            <section className="mx-auto max-w-5xl px-6 pb-section-gap lg:px-margin-page">
                <div className="grid gap-10 md:grid-cols-2">
                    <Card>
                        <h2 className="text-course-title text-on-surface">Learning Outcomes</h2>
                        <ul className="mt-4 space-y-3">
                            {programme.learning_outcomes.map((outcome) => (
                                <li key={outcome} className="flex gap-3 text-sm text-on-surface-variant">
                                    <span className="text-status-published">✓</span>
                                    {outcome}
                                </li>
                            ))}
                        </ul>
                    </Card>
                    <div className="aspect-video rounded-lg bg-jgcp-navy" />
                </div>

                <div className="mt-16">
                    <p className="text-label-caps text-accent-gold">Core Curriculum</p>
                    <h2 className="text-course-title mt-2 text-on-surface">Required Coursework</h2>
                    <p className="text-body-md mt-1 text-on-surface-variant">
                        Progressive modules designed to build systemic mastery of leadership dynamics.
                    </p>

                    <div className="mt-6 grid gap-4 md:grid-cols-3 lg:grid-cols-5">
                        {programme.courses.map((course) => (
                            <Card key={course.id} className="p-4">
                                {course.code && (
                                    <p className="text-label-caps text-accent-gold">{course.code}</p>
                                )}
                                <p className="mt-2 text-sm font-semibold text-on-surface">{course.title}</p>
                                <p className="mt-1 text-xs text-on-surface-variant line-clamp-3">
                                    {course.description}
                                </p>
                            </Card>
                        ))}
                    </div>
                </div>

                {programme.recommended_programmes.length > 0 && (
                    <div className="mt-16">
                        <h2 className="text-course-title text-on-surface">Recommended Next Programme</h2>
                        <div className="mt-4 grid gap-4 md:grid-cols-3">
                            {programme.recommended_programmes.map((rec) => (
                                <Link
                                    key={rec.slug}
                                    href={route('programmes.show', rec.slug)}
                                    className="block rounded-lg border border-outline-variant p-4 hover:border-jgcp-navy"
                                >
                                    <p className="text-xs text-on-surface-variant">{rec.institute_name}</p>
                                    <p className="mt-1 text-sm font-semibold text-on-surface">{rec.title}</p>
                                </Link>
                            ))}
                        </div>
                    </div>
                )}
            </section>

            <section className="bg-jgcp-navy py-section-gap text-white">
                <div className="mx-auto max-w-5xl px-6 lg:px-margin-page">
                    <p className="text-label-caps text-accent-gold">Evaluation &amp; Mastery</p>
                    <h2 className="text-institute-title mt-2 text-white">Assessment Structure</h2>
                    <p className="text-body-md mt-2 max-w-2xl text-white/70">
                        Our certification process goes beyond testing; it is a portfolio-building journey that
                        demonstrates your readiness for global influence.
                    </p>

                    <div className="mt-10 grid gap-8 border-t border-white/10 pt-8 md:grid-cols-4">
                        {programme.assessment_types.map((type) => (
                            <div key={type}>
                                <p className="font-semibold text-white">{assessmentCopy[type]?.label ?? type}</p>
                                <p className="mt-2 text-sm text-white/60">{assessmentCopy[type]?.description}</p>
                            </div>
                        ))}
                    </div>
                </div>
            </section>

            <section className="mx-auto max-w-3xl px-6 py-section-gap text-center lg:px-margin-page">
                <Card className="p-10">
                    <h2 className="text-course-title text-on-surface">Ready to Define Your Influence?</h2>
                    <p className="text-body-md mt-2 text-on-surface-variant">
                        Applications for the current intake are now open. Secure your place among the next
                        generation of responsible global leaders.
                    </p>
                    <div className="mt-6 flex flex-wrap justify-center gap-4">
                        {isEnrolled ? (
                            <Button href={route('student.dashboard')} variant="primary">
                                Go to Dashboard
                            </Button>
                        ) : auth?.user?.role === 'student' ? (
                            <Button
                                href={route('student.programmes.enrol', programme.id)}
                                method="post"
                                variant="primary"
                            >
                                Enrol Now
                            </Button>
                        ) : (
                            <Button href={auth?.user ? route('dashboard') : route('register')} variant="primary">
                                Enrol Now
                            </Button>
                        )}
                        <Button variant="secondary">Download Syllabus</Button>
                    </div>
                </Card>
            </section>
        </PublicLayout>
    );
}
