
from src.aeros_equipment.service import get_asset_batch
from typing import List

from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel
from sqlalchemy import select

from src.aeros_equipment.model import AerosEquipmentCustomParameterData
from src.auth.service import CurrentUser
from src.database.core import DbSession
from src.database.service import CommonParameters
from src.models import StandardResponse
from .schema import CustomParameter, EquipmentPagination
from .service import save_default_equipment, get_all

# from .schema import (OverhaulScheduleCreate, OverhaulSchedulePagination, OverhaulScheduleUpdate)

router = APIRouter()    


@router.get("", response_model=StandardResponse[EquipmentPagination])
async def get_all_simulation(db_session: DbSession, common: CommonParameters):
    """Get all simulation."""

    results = await get_all(common = common)


    return {"data": results, "status": "success", "message": "Success"}



@router.get("/save_default", response_model=StandardResponse[None])
async def save_default_equipments(
    db_session: DbSession, project_name: str = "trialapi"
):
    await save_default_equipment(db_session=db_session, project_name=project_name)

    return {"data": None, "status": "success", "message": "Success"}


@router.get("/parameter-oreda/{location_tag}", response_model=StandardResponse[CustomParameter])
async def get_parameter_oreda(
    db_session: DbSession, location_tag:str
):
    query = select(AerosEquipmentCustomParameterData).where(AerosEquipmentCustomParameterData.location_tag == location_tag).where(AerosEquipmentCustomParameterData.level == "Critical")
    result = await db_session.execute(query)
    
    res = result.scalars().first()
    
    if not res:
        raise HTTPException(status_code=404, detail="Data not found")

    
    return {
        "data" : res,
        "status": "success", 
        "message": "Success"
    }

class AssetBatchRequest(BaseModel):
    repairable_tags: List[str]
    non_repairable_tags: List[str]


@router.post("/test-asset-batch", response_model=StandardResponse[dict])
async def test_asset_batch(
    db_session: DbSession, payload: AssetBatchRequest
):
    equipments = await get_asset_batch(
        payload.repairable_tags,
        payload.non_repairable_tags,
        db_session,
    )

    return {"data": equipments, "status": "success", "message": "Success"}