import { Head, Link } from '@inertiajs/react';
import { useState } from 'react';
import PublicLayout from '@/Layouts/PublicLayout';
import Card from '@/Components/UI/Card';
import Badge from '@/Components/UI/Badge';
import Button from '@/Components/UI/Button';

interface ResourceItem {
    id: number;
    title: string;
    type: string;
    access_level: string;
    description: string | null;
    programmes: string[];
    unlocked: boolean;
}

const typeLabels: Record<string, string> = {
    template: 'Template',
    checklist: 'Checklist',
    assessment_tool: 'Assessment Tool',
    framework: 'Framework',
    policy_template: 'Policy Template',
    diagnostic_tool: 'Diagnostic Tool',
    worksheet: 'Worksheet',
    implementation_guide: 'Implementation Guide',
};

const accessLabels: Record<string, string> = {
    public: 'Public',
    registered_student: 'Sign In Required',
    programme_specific: 'Programme-Linked',
    premium: 'Premium',
    administrative: 'Administrative',
};

export default function Index({
    resources,
    isAuthenticated,
}: {
    resources: ResourceItem[];
    isAuthenticated: boolean;
}) {
    const [type, setType] = useState('');
    const filtered = type ? resources.filter((r) => r.type === type) : resources;

    return (
        <PublicLayout>
            <Head title="Resource Library" />

            <section className="mx-auto max-w-6xl px-6 pb-10 pt-16 lg:px-margin-page">
                <p className="text-label-caps text-accent-gold">Digital Library</p>
                <h1 className="text-institutional-header mt-4 text-on-surface">Resource Library</h1>
                <p className="text-body-lg mt-4 max-w-2xl text-on-surface-variant">
                    Practical leadership templates, checklists, diagnostic tools, and implementation guides —
                    browsable independently of any course or Programme enrolment.
                </p>

                {!isAuthenticated && (
                    <Card className="mt-6 max-w-xl">
                        <p className="text-sm text-on-surface-variant">
                            <Link href={route('login')} className="font-medium text-jgcp-navy hover:underline">
                                Sign in
                            </Link>{' '}
                            to unlock resources reserved for registered students.
                        </p>
                    </Card>
                )}
            </section>

            <section className="mx-auto max-w-6xl px-6 pb-section-gap lg:px-margin-page">
                <div className="flex flex-wrap gap-2">
                    <button
                        onClick={() => setType('')}
                        className={`rounded-full px-4 py-1.5 text-sm font-medium ${
                            type === '' ? 'bg-jgcp-navy text-white' : 'bg-surface-container-high text-on-surface-variant'
                        }`}
                    >
                        All
                    </button>
                    {Object.entries(typeLabels).map(([value, label]) => (
                        <button
                            key={value}
                            onClick={() => setType(value)}
                            className={`rounded-full px-4 py-1.5 text-sm font-medium ${
                                type === value
                                    ? 'bg-jgcp-navy text-white'
                                    : 'bg-surface-container-high text-on-surface-variant'
                            }`}
                        >
                            {label}
                        </button>
                    ))}
                </div>

                <div className="mt-8 grid gap-6 md:grid-cols-3">
                    {filtered.map((resource) => (
                        <Card key={resource.id} className={!resource.unlocked ? 'opacity-70' : ''}>
                            <div className="flex items-center justify-between">
                                <Badge tone="neutral">{typeLabels[resource.type] ?? resource.type}</Badge>
                                <Badge tone={resource.access_level === 'public' ? 'published' : 'gold'}>
                                    {accessLabels[resource.access_level] ?? resource.access_level}
                                </Badge>
                            </div>
                            <h3 className="text-course-title mt-3 text-on-surface">{resource.title}</h3>
                            <p className="text-body-md mt-2 text-on-surface-variant">{resource.description}</p>

                            {resource.programmes.length > 0 && (
                                <p className="mt-3 text-xs text-on-surface-variant">
                                    Linked to: {resource.programmes.join(', ')}
                                </p>
                            )}

                            <div className="mt-5">
                                {resource.unlocked ? (
                                    <Button variant="secondary" className="w-full">
                                        Download
                                    </Button>
                                ) : (
                                    <Button href={route('login')} variant="ghost" className="w-full">
                                        Sign In to Unlock
                                    </Button>
                                )}
                            </div>
                        </Card>
                    ))}

                    {filtered.length === 0 && (
                        <Card className="md:col-span-3">
                            <p className="text-body-md text-on-surface-variant">No resources match this filter.</p>
                        </Card>
                    )}
                </div>
            </section>
        </PublicLayout>
    );
}
