import { useState, useCallback } from 'react';
import type { ProductionOrderDTO, CreateProductionOrderRequest, ProductionOrderFilter } from '@effigenix/api-client';
import { client } from '../utils/api-client.js';

interface ProductionOrdersState {
  productionOrders: ProductionOrderDTO[];
  productionOrder: ProductionOrderDTO | null;
  loading: boolean;
  error: string | null;
}

function errorMessage(err: unknown): string {
  return err instanceof Error ? err.message : 'Unbekannter Fehler';
}

export function useProductionOrders() {
  const [state, setState] = useState<ProductionOrdersState>({
    productionOrders: [],
    productionOrder: null,
    loading: false,
    error: null,
  });

  const fetchProductionOrders = useCallback(async (filter?: ProductionOrderFilter) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrders = await client.productionOrders.list(filter);
      setState((s) => ({ ...s, productionOrders, loading: false, error: null }));
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
    }
  }, []);

  const fetchProductionOrder = useCallback(async (id: string) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrder = await client.productionOrders.getById(id);
      setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
    }
  }, []);

  const createProductionOrder = useCallback(async (request: CreateProductionOrderRequest) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrder = await client.productionOrders.create(request);
      setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
      return productionOrder;
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
      return null;
    }
  }, []);

  const releaseProductionOrder = useCallback(async (id: string) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrder = await client.productionOrders.release(id);
      setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
      return productionOrder;
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
      return null;
    }
  }, []);

  const rescheduleProductionOrder = useCallback(async (id: string, newPlannedDate: string) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrder = await client.productionOrders.reschedule(id, { newPlannedDate });
      setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
      return productionOrder;
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
      return null;
    }
  }, []);

  const startProductionOrder = useCallback(async (id: string) => {
    setState((s) => ({ ...s, loading: true, error: null }));
    try {
      const productionOrder = await client.productionOrders.start(id);
      setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
      return productionOrder;
    } catch (err) {
      setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
      return null;
    }
  }, []);

  const clearError = useCallback(() => {
    setState((s) => ({ ...s, error: null }));
  }, []);

  return {
    ...state,
    fetchProductionOrders,
    fetchProductionOrder,
    createProductionOrder,
    releaseProductionOrder,
    rescheduleProductionOrder,
    startProductionOrder,
    clearError,
  };
}
