"use client";

import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
import { Download, Printer, ArrowLeft } from "lucide-react";
import { useRouter } from "next/navigation";

export default function InvoiceClientPage({ id }: { id: string }) {
  const router = useRouter();

  // Mock invoice data - In production, fetch from API
  const invoice = {
    id: id,
    invoiceNumber: `INV-2024-${id}`,
    date: "December 21, 2024",
    dueDate: "December 21, 2024",
    bookingType: "Vehicle Booking",
    status: "Paid",
    customer: {
      name: "Test Customer",
      email: "info@ameeraholidays.com",
      phone: "+91 91 85059 99770",
      address:
        "First Floor, 7/8, 82, Diving Road, Opp. Milan Sweets Block A, Sector 7, Faridabad, Haryana 121006, India",
      gstin: "27XXXXX1234X1Z5",
    },
    company: {
      name: "Ameera Holidays Travel Services Pvt Ltd",
      address:
        "First Floor, 7/8, 82, Diving Road, Opp. Milan Sweets Block A, Sector 7, Faridabad, Haryana 121006, India",
      phone: "+91 91 85059 99770",
      email: "info@ameeraholidays.com",
      gstin: "27ABCDE1234F1Z5",
      pan: "ABCDE1234F",
    },
    items: [
      {
        description: "Vehicle Booking - Toyota Innova (Mumbai to Pune)",
        quantity: 1,
        rate: 2966.1,
        amount: 2966.1,
      },
    ],
    subtotal: 2966.1,
    cgst: 266.95,
    sgst: 266.95,
    total: 3500,
  };

  const handlePrint = () => {
    window.print();
  };

  return (
    <>
      <style jsx global>{`
        @media print {
          body * {
            visibility: hidden;
          }
          #invoice-content,
          #invoice-content * {
            visibility: visible;
          }
          #invoice-content {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            max-width: 210mm;
            margin: 0 auto;
          }
          .print-hide {
            display: none !important;
          }
          @page {
            size: A4;
            margin: 15mm;
          }
          html,
          body {
            height: 100%;
            margin: 0;
            padding: 0;
          }
        }
      `}</style>

      <div className="py-8 px-4 md:px-6">
        <div className="container max-w-4xl mx-auto">
          <div className="mb-6 flex items-center justify-between print-hide">
            <Button variant="ghost" size="sm" onClick={() => router.back()}>
              <ArrowLeft className="mr-2 h-4 w-4" />
              Back
            </Button>
            <div className="flex gap-2">
              <Button variant="outline" size="sm" onClick={handlePrint}>
                <Printer className="mr-2 h-4 w-4" />
                Print
              </Button>
              <Button size="sm">
                <Download className="mr-2 h-4 w-4" />
                Download PDF
              </Button>
            </div>
          </div>

          <div id="invoice-content">
            <Card className="p-8 md:p-12 shadow-xl print:shadow-none print:border-none">
              {/* Header */}
              <div className="mb-8">
                <div className="flex justify-between items-start mb-6">
                  <div>
                    <h1 className="text-3xl font-bold text-primary mb-2">
                      TAX INVOICE
                    </h1>
                    <p className="text-sm text-muted-foreground">
                      Invoice #{invoice.invoiceNumber}
                    </p>
                  </div>
                  <div className="text-right">
                    <div className="text-2xl font-bold text-primary mb-1">
                      Ameera Holidays
                    </div>
                    <p className="text-sm text-muted-foreground">
                      Travel Services
                    </p>
                  </div>
                </div>
                <Separator />
              </div>

              {/* Company and Customer Info */}
              <div className="grid md:grid-cols-2 gap-8 mb-8">
                <div>
                  <h3 className="font-semibold mb-3 text-sm text-muted-foreground">
                    FROM
                  </h3>
                  <div className="space-y-1">
                    <p className="font-semibold">{invoice.company.name}</p>
                    <p className="text-sm text-muted-foreground">
                      {invoice.company.address}
                    </p>
                    <p className="text-sm">Phone: {invoice.company.phone}</p>
                    <p className="text-sm">Email: {invoice.company.email}</p>
                    <p className="text-sm">GSTIN: {invoice.company.gstin}</p>
                    <p className="text-sm">PAN: {invoice.company.pan}</p>
                  </div>
                </div>
                <div>
                  <h3 className="font-semibold mb-3 text-sm text-muted-foreground">
                    BILL TO
                  </h3>
                  <div className="space-y-1">
                    <p className="font-semibold">{invoice.customer.name}</p>
                    <p className="text-sm text-muted-foreground">
                      {invoice.customer.address}
                    </p>
                    <p className="text-sm">Phone: {invoice.customer.phone}</p>
                    <p className="text-sm">Email: {invoice.customer.email}</p>
                    {invoice.customer.gstin && (
                      <p className="text-sm">GSTIN: {invoice.customer.gstin}</p>
                    )}
                  </div>
                </div>
              </div>

              {/* Invoice Details */}
              <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8 p-4 bg-muted/50 rounded-lg print:bg-gray-50">
                <div>
                  <p className="text-sm text-muted-foreground mb-1">
                    Invoice Date
                  </p>
                  <p className="font-semibold">{invoice.date}</p>
                </div>
                <div>
                  <p className="text-sm text-muted-foreground mb-1">Due Date</p>
                  <p className="font-semibold">{invoice.dueDate}</p>
                </div>
                <div>
                  <p className="text-sm text-muted-foreground mb-1">
                    Booking ID
                  </p>
                  <p className="font-semibold">{invoice.id}</p>
                </div>
                <div>
                  <p className="text-sm text-muted-foreground mb-1">Status</p>
                  <span className="inline-block px-2 py-1 text-xs font-semibold bg-green-100 text-green-800 rounded">
                    {invoice.status}
                  </span>
                </div>
              </div>

              {/* Items Table */}
              <div className="mb-8">
                <table className="w-full">
                  <thead>
                    <tr className="border-b-2 border-border">
                      <th className="text-left py-3 font-semibold">
                        Description
                      </th>
                      <th className="text-right py-3 font-semibold">Qty</th>
                      <th className="text-right py-3 font-semibold">Rate</th>
                      <th className="text-right py-3 font-semibold">Amount</th>
                    </tr>
                  </thead>
                  <tbody>
                    {invoice.items.map((item, index) => (
                      <tr key={index} className="border-b border-border">
                        <td className="py-4">{item.description}</td>
                        <td className="text-right py-4">{item.quantity}</td>
                        <td className="text-right py-4">
                          ₹{item.rate.toFixed(2)}
                        </td>
                        <td className="text-right py-4 font-semibold">
                          ₹{item.amount.toFixed(2)}
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>

              {/* Totals */}
              <div className="flex justify-end mb-8">
                <div className="w-full md:w-1/2 space-y-2">
                  <div className="flex justify-between py-2">
                    <span className="text-muted-foreground">Subtotal:</span>
                    <span className="font-semibold">
                      ₹{invoice.subtotal.toFixed(2)}
                    </span>
                  </div>
                  <div className="flex justify-between py-2">
                    <span className="text-muted-foreground">CGST (9%):</span>
                    <span className="font-semibold">
                      ₹{invoice.cgst.toFixed(2)}
                    </span>
                  </div>
                  <div className="flex justify-between py-2">
                    <span className="text-muted-foreground">SGST (9%):</span>
                    <span className="font-semibold">
                      ₹{invoice.sgst.toFixed(2)}
                    </span>
                  </div>
                  <Separator className="my-2" />
                  <div className="flex justify-between py-2">
                    <span className="text-lg font-bold">Total Amount:</span>
                    <span className="text-lg font-bold text-primary">
                      ₹{invoice.total.toLocaleString("en-IN")}
                    </span>
                  </div>
                </div>
              </div>

              {/* Footer */}
              <div className="border-t pt-6">
                <div className="space-y-2 text-sm text-muted-foreground">
                  <p>
                    <strong>Payment Terms:</strong> Payment due upon receipt
                  </p>
                  <p>
                    <strong>Note:</strong> This is a computer-generated invoice
                    and does not require a physical signature.
                  </p>
                  <p className="mt-4 text-center">
                    Thank you for choosing Ameera Holidays Travel Services!
                  </p>
                </div>
              </div>
            </Card>
          </div>
        </div>
      </div>
    </>
  );
}
