import { Head } from '@inertiajs/react';
import { useState } from 'react';
import StudentLayout from '@/Layouts/StudentLayout';
import Card from '@/Components/UI/Card';
import Badge from '@/Components/UI/Badge';
import StatTile from '@/Components/UI/StatTile';

interface ReferralRow {
    id: number;
    referred_name: string;
    status: 'pending' | 'rewarded';
    reward_amount: string | null;
    created_at: string;
}

interface TransactionRow {
    id: number;
    amount: string;
    description: string | null;
    created_at: string;
}

export default function Referrals({
    isEligible,
    referralLink,
    bonusAmount,
    walletBalance,
    referrals,
    transactions,
}: {
    isEligible: boolean;
    referralCode: string | null;
    referralLink: string | null;
    bonusAmount: string;
    walletBalance: string;
    referrals: ReferralRow[];
    transactions: TransactionRow[];
}) {
    const [copied, setCopied] = useState(false);

    const copyLink = async () => {
        if (!referralLink) return;
        await navigator.clipboard.writeText(referralLink);
        setCopied(true);
        setTimeout(() => setCopied(false), 2000);
    };

    return (
        <StudentLayout>
            <Head title="Refer & Earn" />

            <h1 className="text-institute-title text-on-surface">Refer &amp; Earn</h1>
            <p className="text-body-md text-on-surface-variant">
                Earn ${Number(bonusAmount).toLocaleString()} in your Referral Wallet for every person you refer who
                enrols in a Programme.
            </p>

            {!isEligible ? (
                <Card className="mt-8 max-w-xl">
                    <p className="text-label-caps text-accent-gold">Locked</p>
                    <h2 className="text-course-title mt-2 text-on-surface">Complete a Course to Unlock Referrals</h2>
                    <p className="text-body-md mt-2 text-on-surface-variant">
                        Referral access is unlocked once you complete every lesson in at least one course. Keep
                        learning — your referral link will appear here as soon as you qualify.
                    </p>
                </Card>
            ) : (
                <>
                    <div className="mt-8 grid gap-6 md:grid-cols-2">
                        <StatTile label="Referral Wallet Balance" value={`$${Number(walletBalance).toLocaleString()}`} />
                        <StatTile
                            label="Bonus Per Referral"
                            value={`$${Number(bonusAmount).toLocaleString()}`}
                            hint="Set by JGCP administration"
                        />
                    </div>

                    <Card className="mt-6">
                        <p className="text-label-caps text-accent-gold">Your Referral Link</p>
                        <div className="mt-2 flex flex-wrap items-center gap-3">
                            <code className="rounded bg-surface-container-high px-3 py-2 text-sm text-on-surface">
                                {referralLink}
                            </code>
                            <button
                                onClick={copyLink}
                                className="rounded bg-jgcp-navy px-4 py-2 text-sm font-medium text-white hover:bg-primary-container"
                            >
                                {copied ? 'Copied!' : 'Copy Link'}
                            </button>
                        </div>
                        <p className="mt-3 text-sm text-on-surface-variant">
                            Share this link — when someone registers and enrols in a Programme, your wallet is
                            credited automatically.
                        </p>
                    </Card>

                    <section className="mt-10">
                        <h2 className="text-course-title text-on-surface">People You've Referred</h2>
                        <Card className="mt-4 overflow-x-auto p-0">
                            <table className="w-full text-left text-sm">
                                <thead className="border-b border-outline-variant text-on-surface-variant">
                                    <tr>
                                        <th className="px-6 py-3 font-medium">Name</th>
                                        <th className="px-6 py-3 font-medium">Status</th>
                                        <th className="px-6 py-3 font-medium">Reward</th>
                                        <th className="px-6 py-3 font-medium">Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {referrals.map((referral) => (
                                        <tr key={referral.id} className="border-b border-outline-variant last:border-0">
                                            <td className="px-6 py-3 text-on-surface">{referral.referred_name}</td>
                                            <td className="px-6 py-3">
                                                <Badge tone={referral.status === 'rewarded' ? 'published' : 'draft'}>
                                                    {referral.status}
                                                </Badge>
                                            </td>
                                            <td className="px-6 py-3 text-on-surface-variant">
                                                {referral.reward_amount ? `$${Number(referral.reward_amount).toLocaleString()}` : '—'}
                                            </td>
                                            <td className="px-6 py-3 text-on-surface-variant">{referral.created_at}</td>
                                        </tr>
                                    ))}
                                    {referrals.length === 0 && (
                                        <tr>
                                            <td colSpan={4} className="px-6 py-6 text-center text-on-surface-variant">
                                                You haven't referred anyone yet.
                                            </td>
                                        </tr>
                                    )}
                                </tbody>
                            </table>
                        </Card>
                    </section>

                    <section className="mt-10">
                        <h2 className="text-course-title text-on-surface">Wallet Ledger</h2>
                        <Card className="mt-4 overflow-x-auto p-0">
                            <table className="w-full text-left text-sm">
                                <thead className="border-b border-outline-variant text-on-surface-variant">
                                    <tr>
                                        <th className="px-6 py-3 font-medium">Description</th>
                                        <th className="px-6 py-3 font-medium">Amount</th>
                                        <th className="px-6 py-3 font-medium">Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {transactions.map((transaction) => (
                                        <tr key={transaction.id} className="border-b border-outline-variant last:border-0">
                                            <td className="px-6 py-3 text-on-surface">{transaction.description}</td>
                                            <td className="px-6 py-3 font-medium text-status-published">
                                                +${Number(transaction.amount).toLocaleString()}
                                            </td>
                                            <td className="px-6 py-3 text-on-surface-variant">{transaction.created_at}</td>
                                        </tr>
                                    ))}
                                    {transactions.length === 0 && (
                                        <tr>
                                            <td colSpan={3} className="px-6 py-6 text-center text-on-surface-variant">
                                                No wallet activity yet.
                                            </td>
                                        </tr>
                                    )}
                                </tbody>
                            </table>
                        </Card>
                    </section>
                </>
            )}
        </StudentLayout>
    );
}
