"""
维修管理API路由
"""
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.orm import Session
from app.core.deps import get_db, get_current_user
from app.schemas.maintenance import (
    MaintenanceRecordCreate,
    MaintenanceRecordUpdate,
    MaintenanceRecordStart,
    MaintenanceRecordComplete,
    MaintenanceRecordWithRelations,
    MaintenanceRecordQueryParams,
    MaintenanceStatistics
)
from app.services.maintenance_service import maintenance_service

router = APIRouter()


@router.get("/", response_model=list)
def get_maintenance_records(
    skip: int = Query(0, ge=0, description="跳过条数"),
    limit: int = Query(20, ge=1, le=100, description="返回条数"),
    asset_id: Optional[int] = Query(None, description="资产ID"),
    status: Optional[str] = Query(None, description="状态"),
    fault_type: Optional[str] = Query(None, description="故障类型"),
    priority: Optional[str] = Query(None, description="优先级"),
    maintenance_type: Optional[str] = Query(None, description="维修类型"),
    keyword: Optional[str] = Query(None, description="搜索关键词"),
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取维修记录列表

    - **skip**: 跳过条数
    - **limit**: 返回条数（最大100）
    - **asset_id**: 资产ID筛选
    - **status**: 状态筛选（pending/in_progress/completed/cancelled）
    - **fault_type**: 故障类型筛选（hardware/software/network/other）
    - **priority**: 优先级筛选（low/normal/high/urgent）
    - **maintenance_type**: 维修类型筛选（self_repair/vendor_repair/warranty）
    - **keyword**: 搜索关键词（单号/资产编码/故障描述）
    """
    items, total = maintenance_service.get_records(
        db=db,
        skip=skip,
        limit=limit,
        asset_id=asset_id,
        status=status,
        fault_type=fault_type,
        priority=priority,
        maintenance_type=maintenance_type,
        keyword=keyword
    )
    return items


@router.get("/statistics", response_model=MaintenanceStatistics)
def get_maintenance_statistics(
    asset_id: Optional[int] = Query(None, description="资产ID"),
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取维修统计信息

    - **asset_id**: 资产ID（可选）

    返回维修记录总数、待处理数、维修中数、已完成数等统计信息
    """
    return maintenance_service.get_statistics(db, asset_id)


@router.get("/{record_id}", response_model=dict)
def get_maintenance_record(
    record_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取维修记录详情

    - **record_id**: 维修记录ID

    返回维修记录详情及其关联信息
    """
    return maintenance_service.get_record(db, record_id)


@router.post("/", response_model=dict, status_code=status.HTTP_201_CREATED)
def create_maintenance_record(
    obj_in: MaintenanceRecordCreate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    创建维修记录

    - **asset_id**: 资产ID
    - **fault_description**: 故障描述
    - **fault_type**: 故障类型（hardware/software/network/other）
    - **priority**: 优先级（low/normal/high/urgent）
    - **maintenance_type**: 维修类型（self_repair/vendor_repair/warranty）
    - **vendor_id**: 维修供应商ID（外部维修时必填）
    - **maintenance_cost**: 维修费用
    - **maintenance_result**: 维修结果描述
    - **replaced_parts**: 更换的配件
    - **images**: 维修图片URL（多个逗号分隔）
    - **remark**: 备注
    """
    return maintenance_service.create_record(
        db=db,
        obj_in=obj_in,
        report_user_id=current_user.id,
        creator_id=current_user.id
    )


@router.put("/{record_id}", response_model=dict)
def update_maintenance_record(
    record_id: int,
    obj_in: MaintenanceRecordUpdate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    更新维修记录

    - **record_id**: 维修记录ID
    - **fault_description**: 故障描述
    - **fault_type**: 故障类型
    - **priority**: 优先级
    - **maintenance_type**: 维修类型
    - **vendor_id**: 维修供应商ID
    - **maintenance_cost**: 维修费用
    - **maintenance_result**: 维修结果描述
    - **replaced_parts**: 更换的配件
    - **images**: 维修图片URL
    - **remark**: 备注

    已完成的维修记录不能更新
    """
    return maintenance_service.update_record(
        db=db,
        record_id=record_id,
        obj_in=obj_in,
        updater_id=current_user.id
    )


@router.post("/{record_id}/start", response_model=dict)
def start_maintenance(
    record_id: int,
    start_in: MaintenanceRecordStart,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    开始维修

    - **record_id**: 维修记录ID
    - **maintenance_type**: 维修类型
      - self_repair: 自行维修
      - vendor_repair: 外部维修（需指定vendor_id）
      - warranty: 保修维修
    - **vendor_id**: 维修供应商ID（外部维修时必填）
    - **remark**: 备注

    只有待处理状态的维修记录可以开始维修
    """
    return maintenance_service.start_maintenance(
        db=db,
        record_id=record_id,
        start_in=start_in,
        maintenance_user_id=current_user.id
    )


@router.post("/{record_id}/complete", response_model=dict)
def complete_maintenance(
    record_id: int,
    complete_in: MaintenanceRecordComplete,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    完成维修

    - **record_id**: 维修记录ID
    - **maintenance_result**: 维修结果描述
    - **maintenance_cost**: 维修费用
    - **replaced_parts**: 更换的配件
    - **images**: 维修图片URL
    - **asset_status**: 资产维修后状态（in_stock/in_use）

    只有维修中的记录可以完成
    """
    return maintenance_service.complete_maintenance(
        db=db,
        record_id=record_id,
        complete_in=complete_in,
        maintenance_user_id=current_user.id
    )


@router.post("/{record_id}/cancel", response_model=dict)
def cancel_maintenance(
    record_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    取消维修

    - **record_id**: 维修记录ID

    已完成的维修记录不能取消
    """
    return maintenance_service.cancel_maintenance(db, record_id)


@router.delete("/{record_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_maintenance_record(
    record_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    删除维修记录

    - **record_id**: 维修记录ID

    只能删除待处理或已取消的维修记录
    """
    maintenance_service.delete_record(db, record_id)
    return None


@router.get("/asset/{asset_id}", response_model=list)
def get_asset_maintenance_records(
    asset_id: int,
    skip: int = Query(0, ge=0, description="跳过条数"),
    limit: int = Query(50, ge=1, le=100, description="返回条数"),
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取资产的维修记录

    - **asset_id**: 资产ID
    - **skip**: 跳过条数
    - **limit**: 返回条数
    """
    return maintenance_service.get_asset_records(db, asset_id, skip, limit)
