/**
 * useCreateProduct Hook
 * TanStack Query hooks for creating and managing seller products
 */

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { marketplaceService } from '../../../services/marketplace.service';
import { sellerProductKeys } from './useSellerProducts';
import type {
  CreateProductData,
  SellerProduct,
  ProductDraft,
} from '../../../types/marketplace.types';

// ============================================================================
// Query Keys
// ============================================================================

export const createProductKeys = {
  drafts: () => ['seller', 'drafts'] as const,
  draft: (id: string) => ['seller', 'drafts', id] as const,
};

// ============================================================================
// Hooks
// ============================================================================

/**
 * Hook for creating a new product
 */
export function useCreateProduct() {
  const queryClient = useQueryClient();

  return useMutation<SellerProduct, Error, CreateProductData>({
    mutationFn: (data) => marketplaceService.createProduct(data),
    onSuccess: () => {
      // Invalidate seller products list to include new product
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.all });
    },
  });
}

/**
 * Hook for saving a product draft
 */
export function useSaveDraft() {
  const queryClient = useQueryClient();

  return useMutation<ProductDraft, Error, Partial<CreateProductData>>({
    mutationFn: (data) => marketplaceService.saveProductDraft(data),
    onSuccess: () => {
      // Invalidate drafts
      queryClient.invalidateQueries({ queryKey: createProductKeys.drafts() });
      // Also invalidate products list (drafts appear there)
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.all });
    },
  });
}

/**
 * Hook for updating an existing draft
 */
export function useUpdateDraft() {
  const queryClient = useQueryClient();

  return useMutation<
    ProductDraft,
    Error,
    { id: string; data: Partial<CreateProductData> }
  >({
    mutationFn: ({ id, data }) => marketplaceService.updateProductDraft(id, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({ queryKey: createProductKeys.draft(variables.id) });
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.all });
    },
  });
}

/**
 * Hook for publishing a draft
 */
export function usePublishDraft() {
  const queryClient = useQueryClient();

  return useMutation<SellerProduct, Error, string>({
    mutationFn: (draftId) => marketplaceService.publishDraft(draftId),
    onSuccess: (_, draftId) => {
      // Remove draft from cache
      queryClient.removeQueries({ queryKey: createProductKeys.draft(draftId) });
      // Invalidate all product lists
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.all });
    },
  });
}

/**
 * Hook for deleting a draft
 */
export function useDeleteDraft() {
  const queryClient = useQueryClient();

  return useMutation<void, Error, string>({
    mutationFn: (id) => marketplaceService.deleteProductDraft(id),
    onSuccess: (_, id) => {
      queryClient.removeQueries({ queryKey: createProductKeys.draft(id) });
      queryClient.invalidateQueries({ queryKey: createProductKeys.drafts() });
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.all });
    },
  });
}

/**
 * Hook for uploading product images
 */
export function useUploadProductImage() {
  return useMutation<{ url: string }, Error, File>({
    mutationFn: (file) => marketplaceService.uploadProductImage(file),
  });
}

/**
 * Hook for validating coupon code
 */
export function useValidateCoupon() {
  return useMutation<
    { isValid: boolean; discountPercent?: number; discountAmount?: number; message?: string },
    Error,
    { code: string; productId?: string }
  >({
    mutationFn: ({ code, productId }) =>
      marketplaceService.validateCoupon(code, productId),
  });
}
