import React, { useState } from 'react';
import { format } from 'date-fns';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { ScrollArea } from '@/components/ui/scroll-area';
import { MapPin, Calendar, Building, Gavel, FileText, User, DollarSign, ListChecks, Briefcase } from 'lucide-react';
import { useAuth } from '@/contexts/AuthContext';

// NOTE: These sub-components are referenced but may need to be created separately:
// LegalMatterUpdateForm, LegalMatterUpdatesTimeline

export default function LegalMatterDetailsDialog({ open, onOpenChange, matter }) {
  const { user } = useAuth();
  const [refreshTrigger, setRefreshTrigger] = useState(0);

  if (!matter) return null;

  const handleUpdateSuccess = () => {
    setRefreshTrigger(prev => prev + 1);
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[700px] h-[90vh] flex flex-col p-0 gap-0 overflow-hidden">
        <div className="p-6 border-b shrink-0">
          <DialogHeader>
            <div className="flex justify-between items-start gap-4">
              <div className="space-y-1.5">
                <DialogTitle className="text-xl font-bold">{matter.title}</DialogTitle>
                <div className="flex flex-wrap items-center gap-2">
                  {matter.associations && (
                    <Badge variant="secondary" className="gap-1 font-normal">
                      <Building className="h-3 w-3" />
                      {matter.associations.name}
                    </Badge>
                  )}
                  {matter.status && (
                    <Badge variant="outline" className="border-primary/30 text-primary bg-primary/5">
                      {matter.status.replace(/_/g, ' ')}
                    </Badge>
                  )}
                  <div className="text-xs text-muted-foreground flex items-center gap-1 ml-1">
                    <Calendar className="h-3 w-3" />
                    Created {format(new Date(matter.created_at), 'MMM d, yyyy')}
                  </div>
                </div>
              </div>
            </div>
          </DialogHeader>
        </div>

        <ScrollArea className="flex-1 p-6">
          <div className="space-y-6">
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {matter.category && (
                <div className="bg-muted p-3 rounded-md border">
                  <span className="text-xs font-semibold text-muted-foreground uppercase block mb-1">Case Type</span>
                  <div className="flex items-center gap-2 text-sm">
                    <Briefcase className="h-4 w-4 text-muted-foreground" />
                    {matter.category}
                  </div>
                </div>
              )}
              {matter.case_number && (
                <div className="bg-muted p-3 rounded-md border">
                  <span className="text-xs font-semibold text-muted-foreground uppercase block mb-1">Case No.</span>
                  <div className="flex items-center gap-2 text-sm">
                    <FileText className="h-4 w-4 text-muted-foreground" />
                    {matter.case_number}
                  </div>
                </div>
              )}
              {matter.attorney && (
                <div className="bg-muted p-3 rounded-md border">
                  <span className="text-xs font-semibold text-muted-foreground uppercase block mb-1">Attorney</span>
                  <div className="flex items-center gap-2 text-sm">
                    <User className="h-4 w-4 text-muted-foreground" />
                    {matter.attorney}
                  </div>
                </div>
              )}
            </div>

            {/* Matter Description */}
            {matter.description && (
              <div className="space-y-2">
                <h4 className="text-sm font-semibold flex items-center gap-2">
                  <FileText className="h-4 w-4" /> Description
                </h4>
                <div className="text-sm bg-card p-4 rounded-lg border shadow-sm whitespace-pre-wrap">
                  {matter.description}
                </div>
              </div>
            )}
          </div>
        </ScrollArea>

        <div className="p-4 border-t bg-muted/30 flex justify-end shrink-0">
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            Close
          </Button>
        </div>
      </DialogContent>
    </Dialog>
  );
}
