"use client";

import Button from "@/components/ui/buttons/Button";
import SectionWrapper from "@/components/ui/section/SectionWrapper";
import CustomExpandableDataTable from "@/components/ui/tables/CustomExpandableDataTable";
import { statutoryData } from "@/data/client-master";
import { Plus, ShoppingCart } from "lucide-react";
import { useEffect, useState } from "react";
import { TableColumn } from "react-data-table-component";
import { getExpiryDateStatus } from "@/utils";
import Loader from "@/components/ui/Loader/Loader";
import { useModal } from "@/hooks/useModal";
import StatutoryApproval from "../statory-approval/StatutoryApproval";


export default function Statutory({ clientId }: { clientId: string }) {
  const [loading, setLoading] = useState(true);
  const {
    isOpen,
    open: openModal,
    close: closeModal
  } = useModal();

  useEffect(() => {
    const timer = setTimeout(() => setLoading(false), 500);
    return () => clearTimeout(timer);
  }, []);

  const columns: TableColumn<any>[] = [
    {
      name: "Approval / License",
      selector: (row: any) => row.approvalLicense,
      sortable: true,
      minWidth: "320px",
      grow: 2,
    },
    {
      name: "License No.",
      selector: (row: any) => row.licenseNo,
      sortable: true,
      minWidth: "220px",
    },
    {
      name: "Issued By",
      selector: (row: any) => row.issuedBy,
      sortable: true,
      minWidth: "320px",
    },
    {
      name: "Issued Date",
      selector: (row: any) => row.issuedDate,
      sortable: true,
      width: "140px",
    },
    {
      name: "Expiry Date",
      selector: (row: any) => row.expiryDate,
      sortable: true,
      width: "200px",
      cell: (row: any) => {
        const status = getExpiryDateStatus(row.expiryDate);
        const statusStyles: Record<string, string> = {
          Expired: "bg-red-100 text-red-700",
          "< 30 DAYS": "bg-orange-100 text-orange-700",
        };

        return (
          <div className="flex items-center gap-2">
            {row.expiryDate}
            {status && (
              <span
                className={`px-2 py-1 rounded-md text-xs font-medium whitespace-nowrap ${statusStyles[status] || "bg-gray-100 text-gray-700"
                  }`}
              >
                {status}
              </span>
            )}
          </div>
        );
      },
    },
    {
      name: "Status",
      cell: (row: any) => {
        const statusStyles: Record<string, string> = {
          Valid: "bg-green-100 text-green-700",
          Expired: "bg-red-100 text-red-700",
          "Expiring Soon": "bg-orange-100 text-orange-700",
        };

        return (
          <span
            className={`px-2 py-1 rounded-md text-xs font-medium ${statusStyles[row.status] || "bg-gray-100 text-gray-700"
              }`}
          >
            {row.status}
          </span>
        );
      },
      width: "150px",
    },
    {
      name: "Action",
      cell: () => (
        <button className="text-blue-600 hover:text-blue-800 p-2">
          <ShoppingCart size={18} />
        </button>
      ),
      width: "100px",
      center: true,
    },
  ];


  return (
    <div className="flex-1 overflow-auto px-6 py-5">
      <SectionWrapper
        title="Statutory & Regulatory Approvals"
        button={
          <Button
            name="Add Approval"
            variant="outline"
            size="sm"
            icon={Plus}
            onClick={() => openModal()}
          />
        }
      >
        {loading ? (
          <Loader />
        ) : (
          <>
            <CustomExpandableDataTable
              columns={columns}
              data={statutoryData}
              pagination={false}
            />
          </>
        )}
      </SectionWrapper>
      {
        isOpen && (
          <StatutoryApproval onSave={async (data) => { console.log(data) }} onClose={closeModal} />
        )
      }
    </div>
  );
}
