from typing import List, Optional

from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from apis.base import StandardResponse, standard_response
from db.database import get_db
from entity.device_model_relation import DeviceModelRelationInfo, DeviceModelRelationCreate, DeviceModelRelation
from services.device_model_relation_service import DeviceModelRelationService

from app_instance import get_app

router = APIRouter()
app = get_app()


def get_service():
    return app.state.model_relation_service


@router.get("/list_by_device", response_model=StandardResponse[List[DeviceModelRelationInfo]])
async def list_by_device(
        device_id: int,
        db: AsyncSession = Depends(get_db)):
    service = DeviceModelRelationService(db)
    models = await service.get_device_models(device_id)
    return standard_response(data=list(models))


@router.post("/update_by_device", response_model=StandardResponse[List[DeviceModelRelation]])
async def update_by_device(relation_data: List[DeviceModelRelationCreate],
                           device_id: int = Query(...),
                           service: DeviceModelRelationService = Depends(get_service)):
    relations = await service.update_relations_by_device(device_id, relation_data)
    return standard_response(data=relations)
