import type { MediaItem } from '@/types/media';
import type { EnumOption, Paginated, SelectOption } from '@/types/common';
import type { ProductListItem } from '@/types/product';

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

export type OrderItemListItem = {
    id: number;
    quantity_formatted: string;
    unit_price_formatted: string;
    subtotal_formatted: string;
    product_variant?: {
        id: number;
        name: string;
        product?: {
            id: number;
            name: string;
        };
    };
};

export type OrderStatusAction = {
    status: string;
    label: string;
    destructive: boolean;
    permission: string;
    icon_only: boolean;
};

export type MarketplaceSettingsSnapshot = {
    platform: string;
    is_affiliate: boolean;
    base_amount: number;
    base_amount_formatted?: string;
    total_fee_amount: number;
    total_fee_amount_formatted?: string;
    net_amount: number;
    net_amount_formatted?: string;
    fees: Record<string, {
        scope: string;
        value_type: string;
        value: number;
    }>;
    results: Record<string, {
        amount: number;
        balance_after: number;
    }>;
};

export type OrderListItem = {
    id: number;
    order_number: string;
    channel: string;
    channel_label: string;
    price_type: string;
    price_type_label: string;
    payment_type: string;
    payment_type_label: string;
    is_affiliate: boolean;
    status: string;
    status_label: string;
    is_editable: boolean;
    available_actions: OrderStatusAction[];
    tiktok_order_id: string | null;
    shopee_order_id: string | null;
    subtotal_formatted: string;
    discount_formatted: string;
    nego_price_formatted: string | null;
    total_amount: number;
    total_amount_formatted: string;
    notes: string | null;
    created_at_formatted: string;
    customer?: {
        id: number;
        name: string;
    } | null;
    created_by?: {
        username: string;
        profile?: {
            full_name: string;
        } | null;
    };
    items: OrderItemListItem[];
    marketplace_settings_snapshot?: MarketplaceSettingsSnapshot | null;
};

export type OrderCartItem = {
    product_variant_id: number;
    product_name: string;
    variant_name: string;
    stock_quality: string;
    stock_quality_label?: string;
    quantity: string;
    unit_price: number;
    images?: MediaItem[];
};

export const STOCK_QUALITY_OPTIONS = [
    { value: 'good', label: 'Bagus' },
    { value: 'reject', label: 'Reject' },
] as const;

export const STOCK_QUALITY_OPTIONS_CASHIER = [
    { value: 'retail', label: 'Eceran' },
] as const;

export type OrderEditItem = {
    id: number;
    order_number: string;
    customer_id: number | null;
    marketing_id: number | null;
    channel: string;
    price_type: string;
    payment_type: string;
    is_affiliate: boolean;
    status: string;
    tiktok_order_id: string | null;
    shopee_order_id: string | null;
    discount: number;
    nego_price: number | null;
    notes: string | null;
    photos?: MediaItem | null;
    items: Array<{
        product_variant_id: number;
        product_name: string;
        variant_name: string;
        quantity_input: string;
        stock_quality: string;
        unit_price: number;
        product_variant?: {
            images?: MediaItem[];
        };
    }>;
};

export type OrderDetail = {
    id: number;
    order_number: string;
    channel: string;
    channel_label: string;
    price_type: string;
    price_type_label: string;
    payment_type: string;
    payment_type_label: string;
    is_affiliate: boolean;
    status: string;
    status_label: string;
    is_editable: boolean;
    available_actions: OrderStatusAction[];
    tiktok_order_id: string | null;
    shopee_order_id: string | null;
    subtotal: number;
    subtotal_formatted: string;
    discount: number;
    discount_formatted: string;
    nego_price: number | null;
    nego_price_formatted: string | null;
    total_amount: number;
    total_amount_formatted: string;
    notes: string | null;
    created_at_formatted: string;
    creator_name: string;
    marketing_name: string | null;
    customer?: {
        id: number;
        name: string;
        phone_number: string | null;
        address: string | null;
    } | null;
    created_by?: {
        id: number;
        username: string;
        profile?: {
            full_name: string;
        } | null;
    };
    marketing?: {
        id: number;
        username: string;
        profile?: {
            full_name: string;
        } | null;
    } | null;
    items: Array<{
        id: number;
        product_name: string;
        variant_name: string;
        stock_quality: string;
        stock_quality_label?: string;
        quantity: number;
        quantity_formatted: string;
        unit_price: number;
        unit_price_formatted: string;
        subtotal: number;
        subtotal_formatted: string;
        product_variant?: {
            id: number;
            name: string;
            images?: MediaItem[];
            product?: {
                id: number;
                name: string;
            };
        };
    }>;
    cash_transaction?: {
        id: number;
        amount: number;
        description: string;
        created_at: string;
    } | null;
    photos?: MediaItem | null;
    marketplace_settings_snapshot?: MarketplaceSettingsSnapshot | null;
};

export type PaginatedOrders = Paginated<OrderListItem>;

export type OrderCatalogItem = ProductListItem;
