import AppLayout from '@/Layouts/AppLayout';
import { Link } from '@inertiajs/react';
import { Button } from '@/Components/ui/button';
import { RebateHeaderCard } from '@/Components/MenuCompanies/CompanyRebate/RebateHeaderCard';
import { RebateDetailTable } from '@/Components/MenuCompanies/CompanyRebate/RebateDetailTable';
import { CancelRebateButton } from '@/Components/MenuCompanies/CompanyRebate/CancelRebateButton';
import { GeneratedInvoicesPanel } from '@/Components/MenuCompanies/CompanyRebate/GeneratedInvoicesPanel';

/**
 * View Request SM Detail — legacy companyrebatedetail.php (SM scope, shows invoice link).
 * Shows rebate details + Revise / Re-create / Cancel + Invoice section if SM role.
 * Back link → /company-rebates/view-request-sm
 */
export default function ViewRequestSmDetail({ rebate, details, history, pageTitle, options, generatedInvoices = [] }) {
    return (
        <div className="space-y-6">
            <RebateHeaderCard
                rebate={rebate}
                history={history}
                scope="view-request-sm"
                pageTitle={pageTitle}
                options={options}
            />

            {/* Detail table */}
            <div>
                <h3 className="mb-3 text-sm font-semibold uppercase tracking-wide text-muted-foreground">
                    Company Rebate Resume Details
                </h3>
                <RebateDetailTable
                    validityStart={rebate.validityDateStart}
                    validityEnd={rebate.validityDateEnd} items={details} mode="readonly" statusId={rebate.statusId} />
            </div>

            {/* Invoice section (only for SM role) — legacy companyrebateinvoicelink.php */}
            {options.showInvoice && <GeneratedInvoicesPanel invoices={generatedInvoices} />}

            {/* Action buttons — left-aligned, primary first (locked UI ruling) */}
            <div className="flex flex-wrap items-center gap-3">
                {options.reviseEnabled && (
                    <Button asChild variant="outline">
                        <Link href={route('company-rebates.revise.show', { rebate: rebate.id })}>Revise</Link>
                    </Button>
                )}
                {options.recreateEnabled && (
                    <Button asChild variant="outline">
                        <Link href={route('company-rebates.recreate', { rebate: rebate.id })}>Re-create</Link>
                    </Button>
                )}
                {options.cancelEnabled && (
                    <CancelRebateButton rebateId={rebate.id} scope="view-request-sm" />
                )}
            </div>
        </div>
    );
}

ViewRequestSmDetail.layout = [AppLayout];
