'use server';

import { revalidatePath } from 'next/cache';
import { API_BASE } from '../../../lib/api';
import { getAuthHeaders } from '../../../lib/auth-headers';

export async function updateProduct(formData: FormData) {
  const id = Number(formData.get('id'));
  const name = String(formData.get('name') || '').trim();
  const canonicalName = String(formData.get('canonicalName') || '').trim();
  const category = String(formData.get('category') || '').trim();
  const subcategory = String(formData.get('subcategory') || '').trim();
  const brand = String(formData.get('brand') || '').trim();
  const categoryIdRaw = formData.get('categoryId');
  const categoryId = categoryIdRaw !== '' && categoryIdRaw != null ? Number(categoryIdRaw) : null;

  if (!name) throw new Error('Namn får inte vara tomt.');
  if (name.length > 100) throw new Error('Namn får inte vara längre än 100 tecken.');
  if (canonicalName.length > 100) throw new Error('Canonical name får inte vara längre än 100 tecken.');
  if (category.length > 100) throw new Error('Kategori får inte vara längre än 100 tecken.');
  if (subcategory.length > 100) throw new Error('Underkategori får inte vara längre än 100 tecken.');
  if (brand.length > 100) throw new Error('Varumärke får inte vara längre än 100 tecken.');

  const res = await fetch(`${API_BASE}/api/products/${id}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
    body: JSON.stringify({
      name: name || undefined,
      canonicalName: canonicalName || undefined,
      category: category || null,
      subcategory: subcategory || null,
      brand: brand || null,
      categoryId,
    }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte uppdatera produkt: ${text}`);
  }

  revalidatePath('/admin/products');
}

export async function setProductTags(productId: number, tags: string[]) {
  const res = await fetch(`${API_BASE}/api/products/${productId}/tags`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
    body: JSON.stringify({ tags }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte uppdatera taggar: ${text}`);
  }

  revalidatePath('/admin/products');
}

export async function updateProductWithTags(formData: FormData, tags: string[]) {
  const id = Number(formData.get('id'));
  const name = String(formData.get('name') || '').trim();
  const canonicalName = String(formData.get('canonicalName') || '').trim();
  const category = String(formData.get('category') || '').trim();
  const subcategory = String(formData.get('subcategory') || '').trim();
  const brand = String(formData.get('brand') || '').trim();
  const categoryIdRaw = formData.get('categoryId');
  const categoryId = categoryIdRaw !== '' && categoryIdRaw != null ? Number(categoryIdRaw) : null;

  if (!name) throw new Error('Namn får inte vara tomt.');
  if (name.length > 100) throw new Error('Namn får inte vara längre än 100 tecken.');
  if (canonicalName.length > 100) throw new Error('Canonical name får inte vara längre än 100 tecken.');
  if (category.length > 100) throw new Error('Kategori får inte vara längre än 100 tecken.');
  if (subcategory.length > 100) throw new Error('Underkategori får inte vara längre än 100 tecken.');
  if (brand.length > 100) throw new Error('Varumärke får inte vara längre än 100 tecken.');

  const authHeaders = await getAuthHeaders();
  const res = await fetch(`${API_BASE}/api/products/${id}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json', ...authHeaders },
    body: JSON.stringify({
      name: name || undefined,
      canonicalName: canonicalName || undefined,
      category: category || null,
      subcategory: subcategory || null,
      brand: brand || null,
      categoryId,
    }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte uppdatera produkt: ${text}`);
  }

  const updatedProduct = await res.json();

  const tagsRes = await fetch(`${API_BASE}/api/products/${id}/tags`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json', ...authHeaders },
    body: JSON.stringify({ tags }),
    cache: 'no-store',
  });

  if (!tagsRes.ok) {
    const text = await tagsRes.text();
    throw new Error(`Kunde inte uppdatera taggar: ${text}`);
  }

  // Fetch the complete product
  const fullRes = await fetch(`${API_BASE}/api/products/${id}`, {
    headers: authHeaders,
    cache: 'no-store',
  });
  if (!fullRes.ok) return updatedProduct;
  const result = await fullRes.json();
  return result;
}

export async function deleteProduct(id: number) {
  const res = await fetch(`${API_BASE}/api/products/${id}`, {
    method: 'DELETE',
    headers: { ...(await getAuthHeaders()) },
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte ta bort produkt: ${text}`);
  }
}

export async function resetAllProducts() {
  const res = await fetch(`${API_BASE}/api/products/reset-all`, {
    method: 'POST',
    headers: { ...(await getAuthHeaders()) },
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte återställa produkter: ${text}`);
  }

  revalidatePath('/admin/products');
}

export async function bulkSetCategory(ids: number[], categoryId: number | null) {
  if (ids.length === 0) return;
  const res = await fetch(`${API_BASE}/api/products/bulk-update`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
    body: JSON.stringify({ ids, categoryId }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte uppdatera produkter: ${text}`);
  }
}

export async function suggestProductCategory(productId: number) {
  const res = await fetch(`${API_BASE}/api/products/${productId}/suggest-category`, {
    method: 'GET',
    headers: { ...(await getAuthHeaders()) },
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`AI-kategorisering misslyckades: ${text}`);
  }

  return res.json() as Promise<{
    categoryId: number;
    categoryName: string;
    path: string;
    confidence: 'high' | 'medium' | 'low';
    usedFallback: boolean;
  }>;
}

export async function suggestBulkCategories(productIds?: number[]) {
  const res = await fetch(`${API_BASE}/api/products/ai-categorize-bulk`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
    body: JSON.stringify({ productIds }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Bulk-AI-kategorisering misslyckades: ${text}`);
  }

  return res.json() as Promise<
    {
      productId: number;
      productName: string;
      suggestion: {
        categoryId: number;
        categoryName: string;
        path: string;
        confidence: 'high' | 'medium' | 'low';
        usedFallback: boolean;
      };
    }[]
  >;
}

export async function setProductStatus(id: number, status: 'active' | 'rejected') {
  const res = await fetch(`${API_BASE}/api/products/${id}/status`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json', ...(await getAuthHeaders()) },
    body: JSON.stringify({ status }),
    cache: 'no-store',
  });

  if (!res.ok) {
    const text = await res.text();
    throw new Error(`Kunde inte uppdatera status: ${text}`);
  }

  revalidatePath('/admin/products');
  revalidatePath('/admin/products/pending');
}
