import { fail, redirect } from '@sveltejs/kit';
import { createPart, getPartBySku, listCategories } from '$lib/server/parts.js';
import { recordMovement } from '$lib/server/movements.js';

export function load() {
  return { categories: listCategories() };
}

export const actions = {
  default: async ({ request }) => {
    const form = await request.formData();
    const data = Object.fromEntries(form);
    const errors = validate(data);
    if (errors) return fail(400, { errors, values: data });

    if (getPartBySku(data.sku.trim())) {
      return fail(400, { errors: { sku: 'parts.errors.sku_taken' }, values: data });
    }

    // Save the part with quantity 0, then record an opening "in" movement
    // if the user supplied an initial quantity. This keeps quantity changes
    // funneled exclusively through stock_movements.
    const initialQty = Number(data.quantity_on_hand || 0);
    const id = createPart({ ...data, quantity_on_hand: 0 });
    if (initialQty > 0) {
      recordMovement({
        part_id: id,
        movement_type: 'in',
        quantity: initialQty,
        unit_price: data.cost_price,
        reference: 'OPENING'
      });
    }

    throw redirect(303, `/parts/${id}`);
  }
};

function validate(d) {
  const errors = {};
  if (!d.sku || !d.sku.trim()) errors.sku = 'parts.errors.sku_required';
  if ((!d.name_en || !d.name_en.trim()) && (!d.name_tg || !d.name_tg.trim())) {
    errors.name = 'parts.errors.name_required';
  }
  return Object.keys(errors).length ? errors : null;
}
