import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import { entitySchemas } from '@/lib/entityValidationSchemas';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { SlugField } from '@/components/ui/slug-field';
import { Building2, Save, X } from 'lucide-react';
import { Combobox } from '@/components/ui/combobox';
import { useCompanyHeadquarters } from '@/hooks/useAutocompleteData';
import { useUserRole } from '@/hooks/useUserRole';
import { EntityMultiImageUploader, ImageAssignments } from '@/components/upload/EntityMultiImageUploader';
import { FlexibleDateInput, type DatePrecision } from '@/components/ui/flexible-date-input';
import { submitManufacturerCreation, submitManufacturerUpdate } from '@/lib/entitySubmissionHelpers';
import { useAuth } from '@/hooks/useAuth';
import { toast } from 'sonner';
import { useNavigate } from 'react-router-dom';

type ManufacturerFormData = z.infer<typeof entitySchemas.manufacturer>;

// Input type for the form (before transformation)
type ManufacturerFormInput = {
  name: string;
  slug: string;
  description?: string;
  person_type: 'company' | 'individual' | 'firm' | 'organization';
  website_url?: string;
  founded_year?: string; // Legacy support
  founded_date?: string;
  founded_date_precision?: string;
  headquarters_location?: string;
  images?: {
    uploaded: Array<{
      url: string;
      cloudflare_id?: string;
      file?: any;
      isLocal?: boolean;
      caption?: string;
    }>;
    banner_assignment?: number | null;
    card_assignment?: number | null;
  };
};

interface ManufacturerFormProps {
  onSubmit: (data: ManufacturerFormData) => void;
  onCancel: () => void;
  initialData?: Partial<ManufacturerFormData & {
    id?: string;
    banner_image_url?: string;
    card_image_url?: string;
  }>;
}

export function ManufacturerForm({ onSubmit, onCancel, initialData }: ManufacturerFormProps) {
  const { isModerator } = useUserRole();
  const { headquarters } = useCompanyHeadquarters();
  const { user } = useAuth();
  const navigate = useNavigate();
  const [isSubmitting, setIsSubmitting] = useState(false);
  
  const {
    register,
    handleSubmit,
    setValue,
    watch,
    formState: { errors }
  } = useForm<ManufacturerFormInput>({
    resolver: zodResolver(entitySchemas.manufacturer),
    defaultValues: {
      name: initialData?.name || '',
      slug: initialData?.slug || '',
      description: initialData?.description || '',
      person_type: initialData?.person_type || 'company',
      website_url: initialData?.website_url || '',
      founded_year: initialData?.founded_year ? String(initialData.founded_year) : '',
      founded_date: initialData?.founded_date || (initialData?.founded_year ? `${initialData.founded_year}-01-01` : ''),
      founded_date_precision: initialData?.founded_date_precision || (initialData?.founded_year ? 'year' : 'day'),
      headquarters_location: initialData?.headquarters_location || '',
      images: initialData?.images || { uploaded: [] }
    }
  });


  return (
    <Card>
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          <Building2 className="w-5 h-5" />
          {initialData ? 'Edit Manufacturer' : 'Create New Manufacturer'}
        </CardTitle>
      </CardHeader>
      <CardContent>
        <form onSubmit={handleSubmit(async (data) => {
          if (!user) {
            toast.error('You must be logged in to submit');
            return;
          }
          
          setIsSubmitting(true);
          try {
            if (initialData?.id) {
              await submitManufacturerUpdate(initialData.id, data as unknown as ManufacturerFormData, user.id);
              toast.success('Manufacturer update submitted for review');
            } else {
              await submitManufacturerCreation(data as unknown as ManufacturerFormData, user.id);
              toast.success('Manufacturer submitted for review');
            }
            onCancel();
          } catch (error) {
            console.error('Submission error:', error);
            toast.error(error instanceof Error ? error.message : 'Failed to submit manufacturer');
          } finally {
            setIsSubmitting(false);
          }
        })} className="space-y-6">
          {/* Basic Information */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div className="space-y-2">
              <Label htmlFor="name">Name *</Label>
              <Input
                id="name"
                {...register('name')}
                placeholder="Enter manufacturer name"
              />
              {errors.name && (
                <p className="text-sm text-destructive">{errors.name.message}</p>
              )}
            </div>

            <SlugField
              name={watch('name')}
              slug={watch('slug')}
              onSlugChange={(slug) => setValue('slug', slug)}
              isModerator={isModerator()}
            />
          </div>

          {/* Description */}
          <div className="space-y-2">
            <Label htmlFor="description">Description</Label>
            <Textarea
              id="description"
              {...register('description')}
              placeholder="Describe the manufacturer..."
              rows={3}
            />
          </div>

          {/* Person Type */}
          <div className="space-y-2">
            <Label>Entity Type *</Label>
            <RadioGroup
              value={watch('person_type')}
              onValueChange={(value) => setValue('person_type', value as any)}
              className="grid grid-cols-2 md:grid-cols-4 gap-4"
            >
              <div className="flex items-center space-x-2">
                <RadioGroupItem value="company" id="company" />
                <Label htmlFor="company" className="cursor-pointer">Company</Label>
              </div>
              <div className="flex items-center space-x-2">
                <RadioGroupItem value="individual" id="individual" />
                <Label htmlFor="individual" className="cursor-pointer">Individual</Label>
              </div>
              <div className="flex items-center space-x-2">
                <RadioGroupItem value="firm" id="firm" />
                <Label htmlFor="firm" className="cursor-pointer">Firm</Label>
              </div>
              <div className="flex items-center space-x-2">
                <RadioGroupItem value="organization" id="organization" />
                <Label htmlFor="organization" className="cursor-pointer">Organization</Label>
              </div>
            </RadioGroup>
          </div>

          {/* Additional Details */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            <FlexibleDateInput
              value={watch('founded_date') ? new Date(watch('founded_date')) : undefined}
              precision={(watch('founded_date_precision') as DatePrecision) || 'year'}
              onChange={(date, precision) => {
                setValue('founded_date', date ? date.toISOString().split('T')[0] : undefined);
                setValue('founded_date_precision', precision);
              }}
              label="Founded Date"
              placeholder="Select founded date"
              disableFuture={true}
              fromYear={1800}
            />

            <div className="space-y-2">
              <Label htmlFor="headquarters_location">Headquarters Location</Label>
              <Combobox
                options={headquarters}
                value={watch('headquarters_location')}
                onValueChange={(value) => setValue('headquarters_location', value)}
                placeholder="Select or type location"
                searchPlaceholder="Search locations..."
                emptyText="No locations found"
              />
            </div>
          </div>

          {/* Website */}
          <div className="space-y-2">
            <Label htmlFor="website_url">Website URL</Label>
            <Input
              id="website_url"
              type="url"
              {...register('website_url')}
              placeholder="https://example.com"
            />
            {errors.website_url && (
              <p className="text-sm text-destructive">{errors.website_url.message}</p>
            )}
          </div>

          {/* Images */}
          <EntityMultiImageUploader
            mode={initialData ? 'edit' : 'create'}
            value={watch('images') || { uploaded: [] }}
            onChange={(images) => setValue('images', images)}
            entityType="manufacturer"
            entityId={initialData?.id}
            currentBannerUrl={initialData?.banner_image_url}
            currentCardUrl={initialData?.card_image_url}
          />

          {/* Actions */}
          <div className="flex gap-3 justify-end">
            <Button type="button" variant="outline" onClick={onCancel} disabled={isSubmitting}>
              <X className="w-4 h-4 mr-2" />
              Cancel
            </Button>
            <Button type="submit" disabled={isSubmitting}>
              <Save className="w-4 h-4 mr-2" />
              {isSubmitting ? 'Submitting...' : 'Save Manufacturer'}
            </Button>
          </div>
        </form>
      </CardContent>
    </Card>
  );
}
