import React, { useState } from "react";
import { base44 } from "@/api/base44Client";
import { useQuery } from "@tanstack/react-query";
import { Link } from "react-router-dom";
import { createPageUrl } from "@/utils";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { Building2, Plus, Search, Users, Edit } from "lucide-react";
import PageHeader from "../components/common/PageHeader";

export default function EnterpriseManagement() {
  const [searchTerm, setSearchTerm] = useState("");

  const { data: enterprises = [], isLoading } = useQuery({
    queryKey: ['enterprises'],
    queryFn: () => base44.entities.Enterprise.list('-created_date'),
    initialData: [],
  });

  const filteredEnterprises = enterprises.filter(e =>
    !searchTerm ||
    e.enterprise_name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
    e.enterprise_code?.toLowerCase().includes(searchTerm.toLowerCase())
  );

  return (
    <div className="p-4 md:p-8 bg-slate-50 min-h-screen">
      <div className="max-w-7xl mx-auto">
        <PageHeader
          title="Enterprise Management"
          subtitle={`${filteredEnterprises.length} enterprises • Top-level operators`}
          actions={
            <Link to={createPageUrl("AddEnterprise")}>
              <Button className="bg-[#0A39DF] hover:bg-[#0A39DF]/90">
                <Plus className="w-4 h-4 mr-2" />
                Add Enterprise
              </Button>
            </Link>
          }
        />

        {/* Search */}
        <Card className="mb-6 border-slate-200">
          <CardContent className="p-4">
            <div className="relative">
              <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-slate-400" />
              <Input
                placeholder="Search enterprises..."
                value={searchTerm}
                onChange={(e) => setSearchTerm(e.target.value)}
                className="pl-10"
              />
            </div>
          </CardContent>
        </Card>

        {/* Enterprises Grid */}
        {isLoading ? (
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            {[...Array(4)].map((_, i) => (
              <div key={i} className="h-64 bg-slate-100 animate-pulse rounded-xl" />
            ))}
          </div>
        ) : filteredEnterprises.length > 0 ? (
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            {filteredEnterprises.map((enterprise) => (
              <Card key={enterprise.id} className="border-2 border-slate-200 hover:border-[#0A39DF] hover:shadow-xl transition-all">
                <CardContent className="p-6">
                  <div className="flex items-start gap-4 mb-4">
                    <div className="w-16 h-16 bg-gradient-to-br from-indigo-500 to-indigo-700 rounded-xl flex items-center justify-center text-white font-bold text-2xl">
                      {enterprise.enterprise_code}
                    </div>
                    <div className="flex-1">
                      <h3 className="font-bold text-xl text-[#1C323E] mb-1">
                        {enterprise.enterprise_name}
                      </h3>
                      <p className="text-sm text-slate-500">{enterprise.headquarters_address}</p>
                    </div>
                    <Link to={createPageUrl(`EditEnterprise?id=${enterprise.id}`)}>
                      <Button variant="ghost" size="icon" className="text-slate-400 hover:text-[#0A39DF] hover:bg-blue-50">
                        <Edit className="w-4 h-4" />
                      </Button>
                    </Link>
                  </div>

                  {enterprise.brand_family && enterprise.brand_family.length > 0 && (
                    <div className="mb-4">
                      <p className="text-sm font-semibold text-slate-700 mb-2">Brand Family</p>
                      <div className="flex flex-wrap gap-2">
                        {enterprise.brand_family.map((brand, i) => (
                          <Badge key={i} variant="outline" className="text-xs">
                            {brand}
                          </Badge>
                        ))}
                      </div>
                    </div>
                  )}

                  {enterprise.sector_registry && enterprise.sector_registry.length > 0 && (
                    <div className="flex items-center gap-2 pt-4 border-t border-slate-200">
                      <Users className="w-4 h-4 text-slate-500" />
                      <span className="text-sm text-slate-600">
                        {enterprise.sector_registry.length} Sectors
                      </span>
                    </div>
                  )}
                </CardContent>
              </Card>
            ))}
          </div>
        ) : (
          <Card className="border-slate-200">
            <CardContent className="p-12 text-center">
              <Building2 className="w-16 h-16 mx-auto text-slate-300 mb-4" />
              <h3 className="text-xl font-semibold text-slate-700 mb-2">No Enterprises Found</h3>
              <p className="text-slate-500 mb-6">Add your first enterprise</p>
              <Link to={createPageUrl("AddEnterprise")}>
                <Button className="bg-[#0A39DF] hover:bg-[#0A39DF]/90">
                  <Plus className="w-4 h-4 mr-2" />
                  Add First Enterprise
                </Button>
              </Link>
            </CardContent>
          </Card>
        )}
      </div>
    </div>
  );
}