import type { MediaItem } from '@/types/media';
import type { Paginated, SelectOption } from '@/types/common';
import type { RawMaterialListItem } from '@/types/raw-material';

export type { SelectOption } from '@/types/common';

export type PurchaseItemListItem = {
    id: number;
    raw_material_name: string;
    variant_name: string;
    variant?: string;
    quantity_formatted: string;
    unit_price_formatted: string;
    subtotal_formatted: string;
};

export type PurchaseItemDetail = PurchaseItemListItem & {
    raw_material_id?: number;
    raw_material_name?: string;
    raw_material_unit_label?: string;
    raw_material_price?: {
        id: number;
        variant: string;
        raw_material?: {
            id: number;
            name: string;
            unit_label?: string;
        };
    };
};

export type PurchaseListItem = {
    id: number;
    supplier_name?: string;
    supplier?: {
        id: number;
        name: string;
    } | null;
    subtotal_formatted: string;
    discount_formatted: string;
    shipping_cost: number;
    shipping_cost_formatted: string;
    total_formatted: string;
    notes: string | null;
    created_at_formatted: string;
    created_by_name?: string;
    created_by?: {
        username: string;
        profile?: {
            full_name: string;
        } | null;
    } | null;
    photos?: MediaItem | null;
    items: PurchaseItemDetail[];
    has_pending_request?: boolean;
    pending_request_id?: number;
    pending_request_action?: string;
    pending_request_action_label?: string;
};

export type PurchaseCartItem = {
    raw_material_price_id: number;
    raw_material_name: string;
    variant: string;
    unit_abbreviation: string;
    quantity: string;
    unit_price: number;
    images?: MediaItem[];
};

export type PurchaseFormData = {
    supplier_id: string;
    discount: string;
    notes: string;
    photos: File[];
    remove_media_ids: number[];
    items: PurchaseCartItem[];
};

export type PurchaseEditItem = {
    id: number;
    supplier_id: number;
    discount: number;
    shipping_cost: number;
    notes: string | null;
    photos?: MediaItem | null;
    items: Array<{
        raw_material_price_id: number;
        raw_material_name: string;
        variant_name: string;
        unit_abbreviation: string;
        quantity_input: string;
        unit_price: number;
        raw_material_price?: {
            images?: MediaItem[];
        };
    }>;
};

export type PaginatedPurchases = Paginated<PurchaseListItem>;

export type PurchaseCatalogItem = RawMaterialListItem;
