'use server'

import { createClient } from '@/lib/supabase/server'
import { revalidatePath } from 'next/cache'
import { Category, Product, ProductModule } from '../types'
import { randomUUID } from 'crypto'

export async function getProducts() {
  const supabase = await createClient()
  const { data, error } = await supabase
    .from('products')
    .select('*, category:categories(*), modules:product_modules(*)')
    .order('created_at', { ascending: false })

  if (error) throw error
  return data as Product[]
}

export async function getCategories() {
  const supabase = await createClient()
  const { data, error } = await supabase
    .from('categories')
    .select('*')
    .order('sort_order', { ascending: true })

  if (error) throw error
  return data as Category[]
}

export async function createCategory(category: Omit<Category, 'id' | 'created_at'>) {
  const supabase = await createClient()
  const { data, error } = await supabase
    .from('categories')
    .insert([category])
    .select()
    .single()

  if (error) throw error
  revalidatePath('/admin/categories')
  revalidatePath('/order')
  return data
}

export async function updateCategory(id: string, category: Partial<Category>) {
  const supabase = await createClient()
  const { error } = await supabase
    .from('categories')
    .update(category)
    .eq('id', id)

  if (error) throw error
  revalidatePath('/admin/categories')
  revalidatePath('/order')
}

export async function deleteCategory(id: string) {
  const supabase = await createClient()
  const { error } = await supabase
    .from('categories')
    .delete()
    .eq('id', id)

  if (error) throw error
  revalidatePath('/admin/categories')
  revalidatePath('/order')
}

export async function createProduct(
  product: Omit<Product, 'id' | 'created_at' | 'updated_at'>,
  modules: Omit<ProductModule, 'product_id' | 'created_at'>[]
) {
  const supabase = await createClient()

  // Start a transaction-like approach (Supabase doesn't have cross-table transactions easily in one call via JS client without RPC)
  const { data: newProduct, error: productError } = await supabase
    .from('products')
    .insert([product])
    .select()
    .single()

  if (productError) throw productError

  if (modules.length > 0) {
    const modulesWithId = modules.map(m => ({
      id: m.id || randomUUID(),
      product_id: newProduct.id,
      name: m.name,
      description: m.description || null,
      price: m.price,
      requirements: m.requirements || [],
      exclusions: m.exclusions || [],
    }))
    const { error: modulesError } = await supabase
      .from('product_modules')
      .insert(modulesWithId)

    if (modulesError) throw modulesError
  }

  revalidatePath('/admin/products')
  revalidatePath('/order')
  return newProduct
}

export async function updateProduct(id: string, product: Partial<Product>, modules: any[]) {
  const supabase = await createClient()

  const { error: productError } = await supabase
    .from('products')
    .update(product)
    .eq('id', id)

  if (productError) throw productError

  // For modules, a simple approach is to delete and re-insert or use upsert if they have IDs
  // To keep it simple: Delete all modules and re-insert the current list
  await supabase.from('product_modules').delete().eq('product_id', id)

  if (modules.length > 0) {
    const modulesWithId = modules.map(m => ({
      id: m.id || randomUUID(),
      product_id: id,
      name: m.name,
      description: m.description || null,
      price: m.price,
      requirements: m.requirements || [],
      exclusions: m.exclusions || [],
    }))
    const { error: modulesError } = await supabase
      .from('product_modules')
      .insert(modulesWithId)

    if (modulesError) throw modulesError
  }


  revalidatePath('/admin/products')
  revalidatePath('/order')
}

export async function deleteProduct(id: string) {
  const supabase = await createClient()
  const { error } = await supabase.from('products').delete().eq('id', id)
  if (error) throw error

  revalidatePath('/admin/products')
  revalidatePath('/order')
}
