import { Head } from '@inertiajs/react';
import { TrendingUp } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

type CourseEarning = {
    course_id: number;
    course_title: string;
    sales_count: number;
    gross_revenue: number;
    platform_fee: number;
    net_revenue: number;
};

type Summary = {
    total_gross: number;
    total_net: number;
    total_sales: number;
    platform_fee_percent: number;
};

type Props = { byCourse: CourseEarning[]; summary: Summary };

const breadcrumbs: BreadcrumbItem[] = [{ title: 'Pendapatan', href: '/instructor/earnings' }];

function formatRp(amount: number) {
    return `Rp ${amount.toLocaleString('id-ID')}`;
}

export default function InstructorEarningsIndex({ byCourse, summary }: Props) {
    return (
        <>
            <Head title="Pendapatan Saya" />

            <div className="flex flex-col gap-6 p-4 md:p-6 max-w-3xl">
                <div className="flex items-center gap-3">
                    <div className="flex h-9 w-9 items-center justify-center rounded-lg bg-primary/10">
                        <TrendingUp className="h-5 w-5 text-primary" />
                    </div>
                    <div>
                        <h1 className="text-xl font-bold">Pendapatan Saya</h1>
                        <p className="text-sm text-muted-foreground">
                            Biaya platform {summary.platform_fee_percent}% per transaksi
                        </p>
                    </div>
                </div>

                {/* Summary Cards */}
                <div className="grid grid-cols-3 gap-3">
                    <div className="rounded-xl border bg-card p-4">
                        <p className="text-xs text-muted-foreground mb-1">Total Penjualan</p>
                        <p className="text-2xl font-bold">{summary.total_sales}</p>
                        <p className="text-xs text-muted-foreground">transaksi</p>
                    </div>
                    <div className="rounded-xl border bg-card p-4">
                        <p className="text-xs text-muted-foreground mb-1">Pendapatan Kotor</p>
                        <p className="text-lg font-bold">{formatRp(summary.total_gross)}</p>
                        <p className="text-xs text-muted-foreground">sebelum biaya platform</p>
                    </div>
                    <div className="rounded-xl border bg-card p-4">
                        <p className="text-xs text-muted-foreground mb-1">Pendapatan Bersih</p>
                        <p className="text-lg font-bold text-green-600 dark:text-green-400">{formatRp(summary.total_net)}</p>
                        <p className="text-xs text-muted-foreground">setelah biaya platform</p>
                    </div>
                </div>

                {/* Per-course breakdown */}
                <div className="rounded-xl border bg-card">
                    <div className="border-b px-4 py-3">
                        <p className="font-medium text-sm">Rincian per Course</p>
                    </div>

                    {byCourse.length === 0 ? (
                        <div className="p-8 text-center text-sm text-muted-foreground">
                            Belum ada penjualan course berbayar.
                        </div>
                    ) : (
                        <div className="overflow-x-auto">
                            <table className="w-full text-sm">
                                <thead className="border-b">
                                    <tr>
                                        <th className="px-4 py-3 text-left font-medium text-muted-foreground">Course</th>
                                        <th className="px-4 py-3 text-right font-medium text-muted-foreground">Penjualan</th>
                                        <th className="px-4 py-3 text-right font-medium text-muted-foreground">Kotor</th>
                                        <th className="px-4 py-3 text-right font-medium text-muted-foreground">Biaya Platform</th>
                                        <th className="px-4 py-3 text-right font-medium text-muted-foreground">Bersih</th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y">
                                    {byCourse.map((item) => (
                                        <tr key={item.course_id} className="hover:bg-muted/30">
                                            <td className="px-4 py-3 font-medium">{item.course_title}</td>
                                            <td className="px-4 py-3 text-right">{item.sales_count}x</td>
                                            <td className="px-4 py-3 text-right text-muted-foreground">{formatRp(item.gross_revenue)}</td>
                                            <td className="px-4 py-3 text-right text-red-500">-{formatRp(item.platform_fee)}</td>
                                            <td className="px-4 py-3 text-right font-medium text-green-600 dark:text-green-400">
                                                {formatRp(item.net_revenue)}
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                                <tfoot className="border-t bg-muted/20">
                                    <tr>
                                        <td className="px-4 py-3 font-medium">Total</td>
                                        <td className="px-4 py-3 text-right font-medium">{summary.total_sales}x</td>
                                        <td className="px-4 py-3 text-right text-muted-foreground">{formatRp(summary.total_gross)}</td>
                                        <td className="px-4 py-3 text-right text-red-500">
                                            -{formatRp(summary.total_gross - summary.total_net)}
                                        </td>
                                        <td className="px-4 py-3 text-right font-bold text-green-600 dark:text-green-400">
                                            {formatRp(summary.total_net)}
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    )}
                </div>

                <p className="text-xs text-muted-foreground">
                    Pendapatan dihitung dari pembayaran yang telah diverifikasi admin. Hubungi admin untuk pencairan dana.
                </p>
            </div>
        </>
    );
}

InstructorEarningsIndex.layout = (page: React.ReactNode) => (
    <AppLayout breadcrumbs={breadcrumbs}>{page}</AppLayout>
);
