import { queryOptions } from "@tanstack/react-query";
import { jsonQuery, paginatedJsonQuery } from "../apiAdapter";

export interface OrderItem {
  id?: number;
  description: string;
  item_description?: string;
  quantity: number;
  unit: string;
  unit_price: number;
  is_included_in_total: number | boolean;
}

export interface OrderSection {
  id?: number;
  title: string;
  title_cz?: string;
  content: string;
}

export interface OrderInvoice {
  id: number;
  invoice_number: string;
}

export interface OrderProject {
  id: number;
  project_number: string;
  name: string;
  has_nas_folder?: boolean;
}

export interface OrderData {
  id: number;
  order_number: string;
  quotation_id: number;
  quotation_number: string;
  project_code?: string;
  customer_name: string;
  customer_order_number: string;
  currency: string;
  created_at: string;
  status: string;
  notes: string;
  attachment_name?: string;
  language?: string;
  items: OrderItem[];
  sections: OrderSection[];
  scope_title?: string;
  scope_description?: string;
  valid_transitions?: string[];
  invoice?: OrderInvoice;
  project?: OrderProject;
}

export const orderListOptions = (filters: {
  search?: string;
  sort?: string;
  order?: string;
  page?: number;
  perPage?: number;
  status?: string;
  month?: number;
  year?: number;
}) =>
  queryOptions({
    queryKey: ["orders", "list", filters],
    queryFn: () => {
      const params = new URLSearchParams();
      if (filters.search) params.set("search", filters.search);
      if (filters.sort) params.set("sort", filters.sort);
      if (filters.order) params.set("order", filters.order);
      if (filters.page) params.set("page", String(filters.page));
      if (filters.perPage) params.set("per_page", String(filters.perPage));
      if (filters.status) params.set("status", filters.status);
      if (filters.month) params.set("month", String(filters.month));
      if (filters.year) params.set("year", String(filters.year));
      const qs = params.toString();
      return paginatedJsonQuery(`/api/admin/orders${qs ? `?${qs}` : ""}`);
    },
  });

export interface CurrencyAmount {
  amount: number;
  currency: string;
}

export const orderStatsOptions = (filters: {
  search?: string;
  status?: string;
  month?: number;
  year?: number;
}) =>
  queryOptions({
    queryKey: ["orders", "stats", filters],
    queryFn: async () => {
      const params = new URLSearchParams();
      if (filters.search) params.set("search", filters.search);
      if (filters.status) params.set("status", filters.status);
      if (filters.month) params.set("month", String(filters.month));
      if (filters.year) params.set("year", String(filters.year));
      const qs = params.toString();
      const data = await jsonQuery<{ totals?: CurrencyAmount[] }>(
        `/api/admin/orders/stats${qs ? `?${qs}` : ""}`,
      );
      return data.totals ?? [];
    },
  });

export const orderDetailOptions = (id: string | undefined) =>
  queryOptions({
    queryKey: ["orders", id],
    queryFn: () => jsonQuery<OrderData>(`/api/admin/orders/${id}`),
    enabled: !!id,
  });

export const orderNextNumberOptions = () =>
  queryOptions({
    queryKey: ["orders", "next-number"],
    queryFn: () =>
      jsonQuery<{ next_number?: string; number?: string }>(
        "/api/admin/orders/next-number",
      ),
  });
