import { Head, Link } from '@inertiajs/react';
import {
    ArrowLeft,
    BatteryCharging,
    CalendarDays,
    MapPin,
    PlugZap,
    Sun,
    Wrench,
} from 'lucide-react';
import {
    TicketCard,
    ReportIssueButton,
    StatusPill,
} from '@/components/ticket/ticket-ui';
import { formatDate, formatNumber } from '@/lib/format';
import type { CustomerJobDetail } from '@/types';

export default function TicketJobShow({ job }: { job: CustomerJobDetail }) {
    const specs = [
        {
            label: 'System size',
            value: job.system.system_size_kw
                ? `${formatNumber(job.system.system_size_kw)} kW`
                : null,
        },
        {
            label: 'Panels',
            value: job.system.panel_count
                ? `${job.system.panel_count}${job.system.panel ? ` × ${job.system.panel}` : ''}`
                : job.system.panel,
        },
        { label: 'Inverter', value: job.system.inverter },
        {
            label: 'Serial number',
            value: job.system.inverter_serial,
        },
        {
            label: 'Battery',
            value: job.system.battery
                ? `${job.system.battery}${job.system.battery_capacity_kwh ? ` · ${formatNumber(job.system.battery_capacity_kwh)} kWh` : ''}`
                : null,
        },
        {
            label: 'Export limit',
            value: job.system.export_limit_kw
                ? `${formatNumber(job.system.export_limit_kw)} kW`
                : null,
        },
    ].filter((spec) => spec.value);

    return (
        <>
            <Head title={`System ${job.job_number}`} />

            <div className="mx-auto flex w-full max-w-3xl flex-col gap-5">
                <div>
                    <Link
                        href="/ticket/dashboard"
                        className="inline-flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground"
                    >
                        <ArrowLeft className="size-3.5" />
                        Overview
                    </Link>

                    <p className="mt-3 font-mono text-xs text-muted-foreground">
                        {job.job_number}
                    </p>
                    <h1 className="mt-1 text-2xl font-semibold tracking-tight">
                        {job.title}
                    </h1>

                    <div className="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted-foreground">
                        {job.address ? (
                            <span className="inline-flex items-start gap-1.5">
                                <MapPin className="mt-0.5 size-4 shrink-0" />
                                {job.address}
                            </span>
                        ) : null}
                        <span className="inline-flex items-center gap-1.5">
                            <CalendarDays className="size-4" />
                            {job.install_date
                                ? `Installed ${formatDate(job.install_date)}`
                                : job.status_label}
                        </span>
                    </div>
                </div>

                <TicketCard>
                    <div className="flex flex-wrap items-center justify-between gap-3">
                        <div className="flex items-center gap-3">
                            <span className="flex size-10 items-center justify-center rounded-xl bg-solar-soft text-solar-ink">
                                <Sun className="size-5" />
                            </span>
                            <div>
                                <p className="text-sm font-semibold">
                                    {job.status_label}
                                </p>
                                <p className="mt-0.5 inline-flex items-center gap-1.5 text-xs text-muted-foreground">
                                    <PlugZap className="size-3.5" />
                                    {job.connected
                                        ? 'Connected to the grid and exporting'
                                        : 'Not yet confirmed as connected'}
                                </p>
                            </div>
                        </div>
                        <ReportIssueButton
                            href={`/ticket/tickets/new?job=${job.id}`}
                            label="Report an issue with this system"
                        />
                    </div>
                </TicketCard>

                <TicketCard
                    title="What is installed"
                    description="Useful when you ring the manufacturer, or when a technician visits."
                >
                    {specs.length === 0 ? (
                        <p className="text-sm text-muted-foreground">
                            The equipment list has not been recorded on this job
                            yet. Ask us and we will add it.
                        </p>
                    ) : (
                        <dl className="grid gap-x-6 gap-y-3 sm:grid-cols-2">
                            {specs.map((spec) => (
                                <div
                                    key={spec.label}
                                    className="border-b border-hairline pb-2"
                                >
                                    <dt className="text-[11px] tracking-wide text-muted-foreground uppercase">
                                        {spec.label}
                                    </dt>
                                    <dd className="mt-0.5 text-sm">
                                        {spec.value}
                                    </dd>
                                </div>
                            ))}
                        </dl>
                    )}

                    {job.site?.nmi || job.site?.meter_number ? (
                        <p className="mt-4 text-xs text-muted-foreground">
                            {job.site?.nmi
                                ? `NMI ${job.site.nmi}`
                                : `Meter ${job.site?.meter_number}`}
                        </p>
                    ) : null}
                </TicketCard>

                <TicketCard
                    title="Reports on this system"
                    description="Every issue raised against this installation."
                >
                    {job.tickets.length === 0 ? (
                        <div className="rounded-xl border border-dashed border-hairline px-4 py-6 text-center">
                            <Wrench className="mx-auto size-5 text-muted-foreground" />
                            <p className="mt-2 text-sm font-medium">
                                Nothing reported on this system
                            </p>
                            <p className="mx-auto mt-1 max-w-sm text-xs text-muted-foreground">
                                If something changes, report it and it will be
                                linked here automatically.
                            </p>
                        </div>
                    ) : (
                        <ul className="divide-y divide-hairline">
                            {job.tickets.map((ticket) => (
                                <li key={ticket.id}>
                                    <Link
                                        href={`/ticket/tickets/${ticket.id}`}
                                        className="-mx-3 flex flex-col gap-1 rounded-lg px-3 py-3 hover:bg-muted/60"
                                    >
                                        <div className="flex flex-wrap items-center gap-2">
                                            <span className="font-mono text-xs text-muted-foreground">
                                                {ticket.display_number}
                                            </span>
                                            <StatusPill status={ticket.status} />
                                        </div>
                                        <p className="text-sm font-medium">
                                            {ticket.issue}
                                        </p>
                                        <p className="text-xs text-muted-foreground">
                                            Reported{' '}
                                            {formatDate(ticket.reported_at)}
                                        </p>
                                    </Link>
                                </li>
                            ))}
                        </ul>
                    )}
                </TicketCard>

                <p className="flex items-center gap-1.5 px-1 text-xs text-muted-foreground">
                    <BatteryCharging className="size-3.5" />
                    System details come from our installation records. If
                    something looks wrong, reply on a report or contact the
                    office and we will correct it.
                </p>
            </div>
        </>
    );
}
