from __future__ import annotations

from app.schemas.dashboard import AlertItem, BoardSection, DashboardPayload, FlowStep, OverviewMetric
from app.schemas.domain import CostInsight, InventoryLotSummary, MiniAppField, OperationCostLine, ProductSummary


def get_dashboard_payload() -> DashboardPayload:
    return DashboardPayload(
        company="宁波嘉恒智能科技有限公司",
        overview=[
            OverviewMetric(
                code="sales_open",
                label="未交订单",
                value="8",
                unit="单",
                note="其中 3 单未来 7 天交期",
            ),
            OverviewMetric(
                code="mrp_gap",
                label="待采购材料缺口",
                value="59",
                unit="吨",
                highlight="warning",
                note="SAP440 500*600*2.5 需按 2 吨倍数采购",
            ),
            OverviewMetric(
                code="wip_orders",
                label="在制工单",
                value="6",
                unit="张",
                note="2 张存在工序卡滞",
            ),
            OverviewMetric(
                code="scrap_alerts",
                label="异常报废工序",
                value="2",
                unit="处",
                highlight="danger",
                note="冲压与焊接工序超阈值",
            ),
            OverviewMetric(
                code="waiting_ship",
                label="待发货成品",
                value="3200",
                unit="件",
                note="本体产品优先发货",
            ),
            OverviewMetric(
                code="maintenance_due",
                label="设备待保养",
                value="3",
                unit="台",
                highlight="warning",
                note="含 1 台核心冲床",
            ),
        ],
        alerts=[
            AlertItem(
                level="danger",
                title="冲压工序报废率 4.8%",
                description="工单 MO-202604-003 的冲压工序高于允许报废率 2.0%，建议立即复核模具与板材批次。",
                owner="车间主管",
                action="检查模具并冻结异常批次",
            ),
            AlertItem(
                level="warning",
                title="SAP440 材料仅剩 8 吨可用",
                description="按已排产工单计算，当前库存只能支撑 1.3 天，建议优先下达补料采购。",
                owner="采购员",
                action="生成补料采购单",
            ),
            AlertItem(
                level="warning",
                title="冲床 JH-CY-03 保养到期",
                description="设备已连续运行 286 小时，超过设定保养上限 280 小时。",
                owner="设备管理员",
                action="创建保养工单",
            ),
        ],
        flow_board=[
            BoardSection(
                title="订单到采购",
                items=[
                    FlowStep(
                        step_code="sales",
                        title="订单 SO-202604-001",
                        status="done",
                        summary="产品 本体，下单数量 10000 件，客户交期 2026-04-28。",
                        quantity="10000 件",
                    ),
                    FlowStep(
                        step_code="mrp",
                        title="MRP 物料需求",
                        status="active",
                        summary="计算出 SAP440 钢板理论需求 59000kg，按采购倍数建议下单 60000kg。",
                        quantity="60 吨",
                    ),
                    FlowStep(
                        step_code="purchase",
                        title="采购到货",
                        status="queued",
                        summary="已到货 60000kg，待入库复核与批次挂账。",
                        quantity="60 吨",
                    ),
                ],
            ),
            BoardSection(
                title="领料到完工",
                items=[
                    FlowStep(
                        step_code="issue",
                        title="生产出库",
                        status="active",
                        summary="第一批已领料 22000kg，第二批计划明日发料。",
                        quantity="22 吨",
                    ),
                    FlowStep(
                        step_code="production",
                        title="6 道工序报工",
                        status="active",
                        summary="冲压工序存在异常报废，焊接工序节拍偏慢。",
                        quantity="在制 3800 件",
                    ),
                    FlowStep(
                        step_code="receipt",
                        title="成品待入库",
                        status="queued",
                        summary="首批完工 3200 件，待质检确认后转成品库。",
                        quantity="3200 件",
                    ),
                ],
            ),
        ],
    )


def get_product_overview_summary() -> ProductSummary:
    return ProductSummary(
        product_code="FG-BT-001",
        product_name="本体",
        material_name="SAP440 500*600*2.5 钢板",
        order_qty=10000,
        unit_weight_kg=5.2,
        gross_material_kg_per_piece=5.9,
        planned_material_weight_kg=59000,
        suggested_purchase_weight_kg=60000,
        purchase_multiple_weight_kg=2000,
    )


def get_operation_costs() -> list[OperationCostLine]:
    return [
        OperationCostLine(
            seq_no=10,
            operation_name="开平",
            work_center="板材预处理区",
            std_labor_cost=0.12,
            std_machine_cost=0.18,
            allowed_scrap_rate=0.5,
            reported_good_qty=10000,
            reported_scrap_qty=12,
            abnormal=False,
        ),
        OperationCostLine(
            seq_no=20,
            operation_name="冲压",
            work_center="冲压一区",
            std_labor_cost=0.35,
            std_machine_cost=0.42,
            allowed_scrap_rate=2.0,
            reported_good_qty=9580,
            reported_scrap_qty=420,
            abnormal=True,
        ),
        OperationCostLine(
            seq_no=30,
            operation_name="去毛刺",
            work_center="后处理区",
            std_labor_cost=0.08,
            std_machine_cost=0.06,
            allowed_scrap_rate=0.8,
            reported_good_qty=9520,
            reported_scrap_qty=25,
            abnormal=False,
        ),
        OperationCostLine(
            seq_no=40,
            operation_name="折弯",
            work_center="折弯一区",
            std_labor_cost=0.22,
            std_machine_cost=0.20,
            allowed_scrap_rate=1.0,
            reported_good_qty=9475,
            reported_scrap_qty=18,
            abnormal=False,
        ),
        OperationCostLine(
            seq_no=50,
            operation_name="焊接",
            work_center="焊接二区",
            std_labor_cost=0.48,
            std_machine_cost=0.26,
            allowed_scrap_rate=1.5,
            reported_good_qty=9380,
            reported_scrap_qty=85,
            abnormal=True,
        ),
        OperationCostLine(
            seq_no=60,
            operation_name="检验包装",
            work_center="成检包装区",
            std_labor_cost=0.15,
            std_machine_cost=0.04,
            allowed_scrap_rate=0.2,
            reported_good_qty=9320,
            reported_scrap_qty=8,
            abnormal=False,
        ),
    ]


def get_inventory_lots() -> list[InventoryLotSummary]:
    return [
        InventoryLotSummary(
            lot_no="RM-20260418-01",
            item_name="SAP440 500*600*2.5 钢板",
            warehouse="原材料仓",
            location="A-01-03",
            inbound_weight_kg=30000,
            remaining_weight_kg=12000,
            status="PARTIAL",
        ),
        InventoryLotSummary(
            lot_no="RM-20260418-02",
            item_name="SAP440 500*600*2.5 钢板",
            warehouse="原材料仓",
            location="A-01-04",
            inbound_weight_kg=30000,
            remaining_weight_kg=26000,
            status="AVAILABLE",
        ),
        InventoryLotSummary(
            lot_no="FG-20260420-01",
            item_name="本体",
            warehouse="成品仓",
            location="FG-02-01",
            inbound_weight_kg=16640,
            remaining_weight_kg=16640,
            status="WAIT_SHIP",
        ),
    ]


def get_mini_app_fields() -> list[MiniAppField]:
    return [
        MiniAppField(field_name="report_no", label="报工单号", required=True, description="报工流水唯一编号"),
        MiniAppField(field_name="work_order_no", label="工单号", required=True, description="与 ERP 工单主表关联"),
        MiniAppField(field_name="operation_seq", label="工序序号", required=True, description="对应工艺路线工序"),
        MiniAppField(field_name="employee_code", label="员工编码", required=True, description="报工人员账号"),
        MiniAppField(field_name="equipment_code", label="设备编码", required=False, description="采集实际作业设备"),
        MiniAppField(field_name="start_time", label="开始时间", required=True, description="工序开始加工时间"),
        MiniAppField(field_name="end_time", label="结束时间", required=True, description="工序结束加工时间"),
        MiniAppField(field_name="report_qty", label="报工数量", required=True, description="本次加工总数量"),
        MiniAppField(field_name="good_qty", label="合格数量", required=True, description="用于流转到下道工序"),
        MiniAppField(field_name="scrap_qty", label="报废数量", required=True, description="系统用于异常判定"),
        MiniAppField(field_name="rework_qty", label="返工数量", required=False, description="返工件单独计成本"),
        MiniAppField(field_name="scrap_reason", label="报废原因", required=False, description="用于工序和设备异常归因"),
        MiniAppField(field_name="downtime_minutes", label="停机分钟数", required=False, description="用于设备稼动率分析"),
        MiniAppField(field_name="remark", label="备注", required=False, description="现场异常补充说明"),
    ]


def get_cost_insights() -> list[CostInsight]:
    return [
        CostInsight(category="材料成本", amount=486000.0, ratio=72.4, note="含板材采购与运输"),
        CostInsight(category="人工成本", amount=82100.0, ratio=12.2, note="按工序报工工时归集"),
        CostInsight(category="设备成本", amount=46300.0, ratio=6.9, note="按设备开机工时分摊"),
        CostInsight(category="制造费用", amount=37200.0, ratio=5.5, note="房租、水电、后勤、管理"),
        CostInsight(category="异常损失", amount=20350.0, ratio=3.0, note="报废与返工"),
    ]
