"""
Table Schemas — Inventory Module.

اسکیماهای جدول (لیست) برای ماژول مدیریت انبار و موجودی.
"""

ITEM_TABLE = {
    "id": "inv_item_table",
    "title": "لیست کالاها",
    "endpoint": "/api/v1/inventory/items/",
    "columns": [
        {"key": "item_code", "title": "کد کالا", "sortable": True, "width": 110},
        {"key": "item_name", "title": "نام کالا", "sortable": True},
        {"key": "item_name_fa", "title": "نام فارسی"},
        {"key": "item_type", "title": "نوع", "type": "badge", "badge_map": {
            "RAW_MATERIAL": {"label": "ماده اولیه", "color": "blue"},
            "SEMI_FINISHED": {"label": "نیمه‌ساخته", "color": "orange"},
            "FINISHED_GOOD": {"label": "کالای ساخته", "color": "green"},
            "SERVICE": {"label": "خدمات", "color": "purple"},
            "CONSUMABLE": {"label": "مصرفی", "color": "gray"},
            "ASSET": {"label": "دارایی", "color": "red"},
        }},
        {"key": "item_group_name", "title": "گروه کالا"},
        {"key": "base_uom_name", "title": "واحد پایه"},
        {"key": "abc_classification", "title": "ABC", "type": "badge", "badge_map": {
            "A": {"label": "A", "color": "red"},
            "B": {"label": "B", "color": "orange"},
            "C": {"label": "C", "color": "green"},
        }},
        {"key": "item_status", "title": "وضعیت", "type": "badge", "sortable": True, "badge_map": {
            "ACTIVE": {"label": "فعال", "color": "green"},
            "INACTIVE": {"label": "غیرفعال", "color": "gray"},
            "OBSOLETE": {"label": "منسوخ", "color": "red"},
        }},
        {"key": "batch_managed", "title": "بچ", "type": "boolean", "width": 60},
        {"key": "serial_managed", "title": "سریال", "type": "boolean", "width": 60},
    ],
    "filters": [
        {"key": "item_type", "label": "نوع کالا", "type": "select"},
        {"key": "item_group", "label": "گروه کالا", "type": "lookup"},
        {"key": "item_status", "label": "وضعیت", "type": "select"},
        {"key": "abc_classification", "label": "طبقه‌بندی ABC", "type": "select"},
        {"key": "batch_managed", "label": "مدیریت بچ", "type": "boolean"},
        {"key": "serial_managed", "label": "مدیریت سریال", "type": "boolean"},
    ],
    "search_fields": ["item_code", "item_name", "item_name_fa"],
    "default_sort": "item_code",
    "actions": {
        "row": [
            {"key": "view", "label": "مشاهده", "icon": "eye"},
            {"key": "edit", "label": "ویرایش", "icon": "edit", "permission": "inventory.item.update"},
            {"key": "activate", "label": "فعال‌سازی", "icon": "check-circle", "permission": "inventory.item.manage"},
            {"key": "deactivate", "label": "غیرفعال‌سازی", "icon": "x-circle", "permission": "inventory.item.manage", "confirm": True},
            {"key": "obsolete", "label": "منسوخ", "icon": "archive", "permission": "inventory.item.manage", "confirm": True},
        ],
        "bulk": [
            {"key": "export", "label": "خروجی اکسل", "icon": "download"},
        ],
    },
}

ITEM_GROUP_TABLE = {
    "id": "inv_item_group_table",
    "title": "گروه‌های کالا",
    "endpoint": "/api/v1/inventory/item-groups/",
    "columns": [
        {"key": "code", "title": "کد گروه", "sortable": True, "width": 100},
        {"key": "name", "title": "نام گروه", "sortable": True},
        {"key": "parent_name", "title": "گروه مادر"},
        {"key": "level", "title": "سطح", "width": 60},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "search_fields": ["code", "name"],
    "default_sort": "code",
}

UNIT_OF_MEASURE_TABLE = {
    "id": "inv_unit_of_measure_table",
    "title": "واحدهای اندازه‌گیری",
    "endpoint": "/api/v1/inventory/units/",
    "columns": [
        {"key": "code", "title": "کد", "sortable": True, "width": 80},
        {"key": "name", "title": "نام", "sortable": True},
        {"key": "name_fa", "title": "نام فارسی"},
        {"key": "uom_type", "title": "نوع", "type": "badge", "badge_map": {
            "WEIGHT": {"label": "وزن", "color": "blue"},
            "LENGTH": {"label": "طول", "color": "green"},
            "VOLUME": {"label": "حجم", "color": "orange"},
            "AREA": {"label": "سطح", "color": "purple"},
            "COUNT": {"label": "تعداد", "color": "gray"},
            "PACK": {"label": "بسته", "color": "red"},
        }},
        {"key": "decimal_places", "title": "اعشار", "width": 60},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "filters": [
        {"key": "uom_type", "label": "نوع واحد", "type": "select"},
    ],
    "search_fields": ["code", "name", "name_fa"],
    "default_sort": "code",
}

WAREHOUSE_TABLE = {
    "id": "inv_warehouse_table",
    "title": "انبارها",
    "endpoint": "/api/v1/inventory/warehouses/",
    "columns": [
        {"key": "warehouse_code", "title": "کد انبار", "sortable": True, "width": 100},
        {"key": "warehouse_name", "title": "نام انبار", "sortable": True},
        {"key": "warehouse_type", "title": "نوع", "type": "badge", "badge_map": {
            "MAIN": {"label": "اصلی", "color": "blue"},
            "TEMPORARY": {"label": "موقت", "color": "orange"},
            "TRANSIT": {"label": "ترانزیت", "color": "purple"},
            "QUARANTINE": {"label": "قرنطینه", "color": "red"},
        }},
        {"key": "city", "title": "شهر"},
        {"key": "manager_name", "title": "مدیر انبار"},
        {"key": "allow_negative_stock", "title": "موجودی منفی", "type": "boolean", "width": 90},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "filters": [
        {"key": "warehouse_type", "label": "نوع انبار", "type": "select"},
        {"key": "is_active", "label": "وضعیت", "type": "boolean"},
    ],
    "search_fields": ["warehouse_code", "warehouse_name", "city"],
    "default_sort": "warehouse_code",
    "actions": {
        "row": [
            {"key": "view", "label": "مشاهده", "icon": "eye"},
            {"key": "edit", "label": "ویرایش", "icon": "edit", "permission": "inventory.warehouse.update"},
            {"key": "stock_summary", "label": "موجودی", "icon": "chart-bar"},
            {"key": "delete", "label": "حذف", "icon": "trash", "permission": "inventory.warehouse.manage", "confirm": True},
        ],
    },
}

STORAGE_LOCATION_TABLE = {
    "id": "inv_storage_location_table",
    "title": "مکان‌های انبار",
    "endpoint": "/api/v1/inventory/storage-locations/",
    "columns": [
        {"key": "location_code", "title": "کد مکان", "sortable": True, "width": 100},
        {"key": "location_name", "title": "نام مکان", "sortable": True},
        {"key": "warehouse_name", "title": "انبار"},
        {"key": "location_type", "title": "نوع", "type": "badge", "badge_map": {
            "ZONE": {"label": "ناحیه", "color": "blue"},
            "AISLE": {"label": "راهرو", "color": "green"},
            "RACK": {"label": "قفسه", "color": "orange"},
            "SHELF": {"label": "طبقه", "color": "purple"},
            "BIN": {"label": "سلول", "color": "gray"},
        }},
        {"key": "is_picking", "title": "برداشت", "type": "boolean", "width": 60},
        {"key": "is_putaway", "title": "چینش", "type": "boolean", "width": 60},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "filters": [
        {"key": "warehouse", "label": "انبار", "type": "lookup"},
        {"key": "location_type", "label": "نوع مکان", "type": "select"},
    ],
    "search_fields": ["location_code", "location_name"],
    "default_sort": "location_code",
}

BATCH_TABLE = {
    "id": "inv_batch_table",
    "title": "بچ‌ها",
    "endpoint": "/api/v1/inventory/batches/",
    "columns": [
        {"key": "batch_number", "title": "شماره بچ", "sortable": True, "width": 120},
        {"key": "item_name", "title": "کالا"},
        {"key": "manufacturing_date", "title": "تاریخ تولید", "type": "date_jalali", "sortable": True},
        {"key": "expiry_date", "title": "تاریخ انقضا", "type": "date_jalali", "sortable": True},
        {"key": "batch_status", "title": "وضعیت بچ", "type": "badge", "badge_map": {
            "AVAILABLE": {"label": "موجود", "color": "green"},
            "RESTRICTED": {"label": "محدود", "color": "orange"},
            "BLOCKED": {"label": "مسدود", "color": "red"},
            "EXPIRED": {"label": "منقضی", "color": "gray"},
        }},
        {"key": "quality_status", "title": "وضعیت کیفی", "type": "badge", "badge_map": {
            "PENDING": {"label": "در انتظار", "color": "orange"},
            "APPROVED": {"label": "تأیید", "color": "green"},
            "REJECTED": {"label": "رد", "color": "red"},
            "CONDITIONAL": {"label": "مشروط", "color": "blue"},
        }},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "batch_status", "label": "وضعیت بچ", "type": "select"},
        {"key": "quality_status", "label": "وضعیت کیفی", "type": "select"},
    ],
    "search_fields": ["batch_number"],
    "default_sort": "-manufacturing_date",
}

SERIAL_NUMBER_TABLE = {
    "id": "inv_serial_number_table",
    "title": "شماره سریال‌ها",
    "endpoint": "/api/v1/inventory/serial-numbers/",
    "columns": [
        {"key": "serial_number", "title": "شماره سریال", "sortable": True, "width": 140},
        {"key": "item_name", "title": "کالا"},
        {"key": "current_warehouse_name", "title": "انبار فعلی"},
        {"key": "current_status", "title": "وضعیت فعلی", "type": "badge", "badge_map": {
            "IN_STOCK": {"label": "موجود", "color": "green"},
            "SOLD": {"label": "فروخته‌شده", "color": "blue"},
            "IN_TRANSIT": {"label": "در حال انتقال", "color": "orange"},
            "IN_SERVICE": {"label": "در سرویس", "color": "purple"},
            "RETURNED": {"label": "مرجوعی", "color": "red"},
            "SCRAPPED": {"label": "ضایعات", "color": "gray"},
        }},
        {"key": "ownership_type", "title": "مالکیت", "type": "badge", "badge_map": {
            "OWNED": {"label": "خودی", "color": "blue"},
            "CONSIGNMENT": {"label": "امانی", "color": "orange"},
            "CUSTOMER": {"label": "مشتری", "color": "green"},
        }},
        {"key": "warranty_end_date", "title": "پایان گارانتی", "type": "date_jalali"},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "current_status", "label": "وضعیت", "type": "select"},
        {"key": "current_warehouse", "label": "انبار", "type": "lookup"},
        {"key": "ownership_type", "label": "مالکیت", "type": "select"},
    ],
    "search_fields": ["serial_number"],
    "default_sort": "-created_at",
}

STOCK_LEDGER_TABLE = {
    "id": "inv_stock_ledger_table",
    "title": "دفتر موجودی",
    "endpoint": "/api/v1/inventory/stock-ledger/",
    "columns": [
        {"key": "posting_number", "title": "شماره ثبت", "sortable": True, "width": 120},
        {"key": "posting_date", "title": "تاریخ", "type": "date_jalali", "sortable": True},
        {"key": "item_name", "title": "کالا"},
        {"key": "warehouse_name", "title": "انبار"},
        {"key": "transaction_type", "title": "نوع", "type": "badge", "badge_map": {
            "RECEIPT": {"label": "ورود", "color": "green"},
            "ISSUE": {"label": "خروج", "color": "red"},
            "TRANSFER": {"label": "انتقال", "color": "blue"},
            "ADJUSTMENT": {"label": "تعدیل", "color": "orange"},
            "RETURN": {"label": "برگشت", "color": "purple"},
            "SCRAP": {"label": "ضایعات", "color": "gray"},
        }},
        {"key": "quantity", "title": "مقدار", "type": "number"},
        {"key": "unit_cost", "title": "بهای واحد", "type": "currency"},
        {"key": "total_cost", "title": "بهای کل", "type": "currency"},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "warehouse", "label": "انبار", "type": "lookup"},
        {"key": "transaction_type", "label": "نوع تراکنش", "type": "select"},
        {"key": "posting_date__gte", "label": "از تاریخ", "type": "date"},
        {"key": "posting_date__lte", "label": "تا تاریخ", "type": "date"},
    ],
    "search_fields": ["posting_number"],
    "default_sort": "-posting_date",
}

STOCK_BALANCE_TABLE = {
    "id": "inv_stock_balance_table",
    "title": "مانده موجودی",
    "endpoint": "/api/v1/inventory/stock-balance/",
    "columns": [
        {"key": "item_name", "title": "کالا"},
        {"key": "warehouse_name", "title": "انبار"},
        {"key": "quantity_on_hand", "title": "موجودی کل", "type": "number", "sortable": True},
        {"key": "reserved_qty", "title": "رزرو", "type": "number"},
        {"key": "allocated_qty", "title": "تخصیص", "type": "number"},
        {"key": "available_qty", "title": "در دسترس", "type": "number", "sortable": True},
        {"key": "in_transit_qty", "title": "در راه", "type": "number"},
        {"key": "avg_unit_cost", "title": "میانگین بها", "type": "currency"},
        {"key": "total_value", "title": "ارزش کل", "type": "currency", "sortable": True},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "warehouse", "label": "انبار", "type": "lookup"},
    ],
    "search_fields": ["item__item_name"],
    "default_sort": "item__item_name",
}

RESERVATION_TABLE = {
    "id": "inv_reservation_table",
    "title": "رزرو موجودی",
    "endpoint": "/api/v1/inventory/reservations/",
    "columns": [
        {"key": "reservation_number", "title": "شماره رزرو", "sortable": True, "width": 120},
        {"key": "reservation_date", "title": "تاریخ", "type": "date_jalali", "sortable": True},
        {"key": "item_name", "title": "کالا"},
        {"key": "warehouse_name", "title": "انبار"},
        {"key": "reserved_qty", "title": "مقدار رزرو", "type": "number"},
        {"key": "allocated_qty", "title": "تخصیص‌یافته", "type": "number"},
        {"key": "status", "title": "وضعیت", "type": "badge", "badge_map": {
            "ACTIVE": {"label": "فعال", "color": "green"},
            "PARTIALLY_ALLOCATED": {"label": "تخصیص جزئی", "color": "orange"},
            "FULLY_ALLOCATED": {"label": "تخصیص کامل", "color": "blue"},
            "CANCELLED": {"label": "لغو", "color": "red"},
            "EXPIRED": {"label": "منقضی", "color": "gray"},
        }},
        {"key": "priority", "title": "اولویت", "width": 70},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "warehouse", "label": "انبار", "type": "lookup"},
        {"key": "status", "label": "وضعیت", "type": "select"},
    ],
    "search_fields": ["reservation_number"],
    "default_sort": "-reservation_date",
}

PHYSICAL_INVENTORY_TABLE = {
    "id": "inv_physical_inventory_table",
    "title": "شمارش فیزیکی",
    "endpoint": "/api/v1/inventory/physical-inventories/",
    "columns": [
        {"key": "count_number", "title": "شماره شمارش", "sortable": True, "width": 120},
        {"key": "count_date", "title": "تاریخ", "type": "date_jalali", "sortable": True},
        {"key": "count_type", "title": "نوع", "type": "badge", "badge_map": {
            "FULL": {"label": "کامل", "color": "blue"},
            "PARTIAL": {"label": "جزئی", "color": "orange"},
            "CYCLE": {"label": "دوره‌ای", "color": "green"},
        }},
        {"key": "warehouse_name", "title": "انبار"},
        {"key": "status", "title": "وضعیت", "type": "badge", "badge_map": {
            "PLANNED": {"label": "برنامه‌ریزی", "color": "gray"},
            "IN_PROGRESS": {"label": "در حال اجرا", "color": "blue"},
            "COUNTING": {"label": "شمارش", "color": "orange"},
            "REVIEW": {"label": "بررسی", "color": "purple"},
            "COMPLETED": {"label": "تکمیل", "color": "green"},
        }},
        {"key": "blind_count", "title": "شمارش کور", "type": "boolean", "width": 80},
        {"key": "responsible_name", "title": "مسئول"},
    ],
    "filters": [
        {"key": "warehouse", "label": "انبار", "type": "lookup"},
        {"key": "count_type", "label": "نوع", "type": "select"},
        {"key": "status", "label": "وضعیت", "type": "select"},
    ],
    "search_fields": ["count_number"],
    "default_sort": "-count_date",
}

INSPECTION_LOT_TABLE = {
    "id": "inv_inspection_lot_table",
    "title": "لات‌های بازرسی",
    "endpoint": "/api/v1/inventory/inspection-lots/",
    "columns": [
        {"key": "lot_number", "title": "شماره لات", "sortable": True, "width": 120},
        {"key": "inspection_type", "title": "نوع", "type": "badge", "badge_map": {
            "INCOMING": {"label": "ورودی", "color": "blue"},
            "IN_PROCESS": {"label": "حین فرآیند", "color": "orange"},
            "FINAL": {"label": "نهایی", "color": "green"},
        }},
        {"key": "item_name", "title": "کالا"},
        {"key": "inspection_qty", "title": "مقدار بازرسی", "type": "number"},
        {"key": "status", "title": "وضعیت", "type": "badge", "badge_map": {
            "CREATED": {"label": "ایجاد‌شده", "color": "gray"},
            "IN_PROGRESS": {"label": "در حال اجرا", "color": "blue"},
            "COMPLETED": {"label": "تکمیل‌شده", "color": "green"},
        }},
        {"key": "quality_decision", "title": "تصمیم کیفی", "type": "badge", "badge_map": {
            "PENDING": {"label": "در انتظار", "color": "gray"},
            "ACCEPT": {"label": "پذیرش", "color": "green"},
            "REJECT": {"label": "رد", "color": "red"},
            "CONDITIONAL": {"label": "مشروط", "color": "orange"},
            "USE_AS_IS": {"label": "استفاده‌ فعلی", "color": "blue"},
            "RETURN_TO_VENDOR": {"label": "برگشت به تأمین‌کننده", "color": "purple"},
        }},
        {"key": "inspector_name", "title": "بازرس"},
    ],
    "filters": [
        {"key": "inspection_type", "label": "نوع بازرسی", "type": "select"},
        {"key": "status", "label": "وضعیت", "type": "select"},
        {"key": "quality_decision", "label": "تصمیم کیفی", "type": "select"},
        {"key": "item", "label": "کالا", "type": "lookup"},
    ],
    "search_fields": ["lot_number"],
    "default_sort": "-inspection_date",
}

COSTING_METHOD_TABLE = {
    "id": "inv_costing_method_table",
    "title": "روش‌های قیمت‌گذاری",
    "endpoint": "/api/v1/inventory/costing-methods/",
    "columns": [
        {"key": "code", "title": "کد", "sortable": True, "width": 80},
        {"key": "name", "title": "نام", "sortable": True},
        {"key": "description", "title": "توضیحات"},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "search_fields": ["code", "name"],
    "default_sort": "code",
}

STOCK_STATUS_TABLE = {
    "id": "inv_stock_status_table",
    "title": "وضعیت‌های موجودی",
    "endpoint": "/api/v1/inventory/stock-statuses/",
    "columns": [
        {"key": "status_code", "title": "کد", "sortable": True, "width": 80},
        {"key": "status_name", "title": "نام", "sortable": True},
        {"key": "status_type", "title": "نوع", "type": "badge", "badge_map": {
            "UNRESTRICTED": {"label": "آزاد", "color": "green"},
            "QUALITY_INSPECTION": {"label": "بازرسی کیفی", "color": "orange"},
            "BLOCKED": {"label": "مسدود", "color": "red"},
            "IN_TRANSIT": {"label": "در حال انتقال", "color": "blue"},
            "RESERVED": {"label": "رزرو", "color": "purple"},
            "DAMAGED": {"label": "آسیب‌دیده", "color": "red"},
            "RETURN": {"label": "مرجوعی", "color": "orange"},
            "SCRAP": {"label": "ضایعات", "color": "gray"},
            "CUSTOM": {"label": "سفارشی", "color": "blue"},
        }},
        {"key": "is_saleable", "title": "قابل فروش", "type": "boolean", "width": 80},
        {"key": "is_blocked_for_movement", "title": "مسدود", "type": "boolean", "width": 80},
        {"key": "is_active", "title": "فعال", "type": "boolean"},
    ],
    "search_fields": ["status_code", "status_name"],
    "default_sort": "sort_order",
}

ITEM_COSTING_TABLE = {
    "id": "inv_item_costing_table",
    "title": "بهای تمام‌شده کالاها",
    "endpoint": "/api/v1/inventory/item-costing/",
    "columns": [
        {"key": "item_name", "title": "کالا"},
        {"key": "costing_method_name", "title": "روش قیمت‌گذاری"},
        {"key": "standard_cost", "title": "بهای استاندارد", "type": "currency"},
        {"key": "moving_average_cost", "title": "میانگین متحرک", "type": "currency"},
        {"key": "last_purchase_cost", "title": "آخرین خرید", "type": "currency"},
        {"key": "last_cost_update", "title": "آخرین بروزرسانی", "type": "date_jalali"},
    ],
    "filters": [
        {"key": "item", "label": "کالا", "type": "lookup"},
        {"key": "costing_method", "label": "روش قیمت‌گذاری", "type": "lookup"},
    ],
    "default_sort": "item__item_name",
}
