import React, { useRef, useEffect } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate, useParams } from 'react-router-dom';
import { api } from '@/lib/api';
import { __ } from '@/lib/i18n';
import { toast } from 'sonner';
import { useFABConfig } from '@/hooks/useFABConfig';
import { usePageHeader } from '@/contexts/PageHeaderContext';
import { ProductFormTabbed as ProductForm, ProductFormData } from './partials/ProductFormTabbed';
import { Button } from '@/components/ui/button';
import { ErrorCard } from '@/components/ErrorCard';
import { getPageLoadErrorMessage } from '@/lib/errorHandling';
import { Skeleton } from '@/components/ui/skeleton';

export default function ProductEdit() {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const formRef = useRef<HTMLFormElement>(null);
  const { setPageHeader, clearPageHeader } = usePageHeader();

  // Hide FAB on edit product page
  useFABConfig('none');

  // Fetch product
  const productQ = useQuery({
    queryKey: ['products', id],
    queryFn: () => api.get(`/products/${id}`),
    enabled: !!id,
  });

  // Update mutation
  const updateMutation = useMutation({
    mutationFn: async (data: ProductFormData) => {
      return api.put(`/products/${id}`, data);
    },
    onSuccess: (response: any) => {
      toast.success(__('Product updated successfully'));
      queryClient.invalidateQueries({ queryKey: ['products'] });
      queryClient.invalidateQueries({ queryKey: ['products', id] });
      
      // Navigate back to products list
      navigate('/products');
    },
    onError: (error: any) => {
      toast.error(error.message || __('Failed to update product'));
    },
  });

  const handleSubmit = async (data: ProductFormData) => {
    await updateMutation.mutateAsync(data);
  };

  // Set page header with back button and save button
  useEffect(() => {
    const actions = (
      <div className="flex gap-2">
        <Button size="sm" variant="ghost" onClick={() => navigate('/products')}>
          {__('Back')}
        </Button>
        <Button 
          size="sm" 
          onClick={() => formRef.current?.requestSubmit()}
          disabled={updateMutation.isPending || productQ.isLoading}
        >
          {updateMutation.isPending ? __('Saving...') : __('Save')}
        </Button>
      </div>
    );
    setPageHeader(__('Edit Product'), actions);
    return () => clearPageHeader();
  }, [updateMutation.isPending, productQ.isLoading, setPageHeader, clearPageHeader, navigate]);

  // Loading state
  if (productQ.isLoading) {
    return (
      <div className="space-y-4">
        <Skeleton className="h-12 w-full" />
        <Skeleton className="h-64 w-full" />
        <Skeleton className="h-32 w-full" />
      </div>
    );
  }

  // Error state
  if (productQ.isError) {
    return (
      <ErrorCard
        title={__('Failed to load product')}
        message={getPageLoadErrorMessage(productQ.error)}
        onRetry={() => productQ.refetch()}
      />
    );
  }

  const product = productQ.data;

  return (
    <div className="space-y-4">
      <ProductForm
        mode="edit"
        initial={product}
        onSubmit={handleSubmit}
        formRef={formRef}
        hideSubmitButton={true}
      />
    </div>
  );
}
