from typing import List
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from app.core.entities.accessory import AccessoryEntity
from app.core.usecases.accessory_service import AccessoriesService
from app.infrastructure.database.dependencies import get_db
from app.core.usecases.auth_service import verify_token
from app.infrastructure.database.models.users import User

router = APIRouter()


@router.get("/accessories", response_model=List[AccessoryEntity])
def read_accessories(db: Session = Depends(get_db), current_user: User = Depends(verify_token)):
    service = AccessoriesService(db)
    return service.get_all_accessories()


@router.get("/accessories/{accessory_id}", response_model=AccessoryEntity)
def read_accessory(accessory_id: int, db: Session = Depends(get_db), current_user: User = Depends(verify_token)):
    service = AccessoriesService(db)
    accessory = service.get_accessory_by_id(accessory_id)
    if accessory is None:
        raise HTTPException(status_code=404, detail="Accessory not found")
    return accessory


@router.post("/accessories", response_model=AccessoryEntity)
def create_accessory(accessory: AccessoryEntity, db: Session = Depends(get_db),
                     current_user: User = Depends(verify_token)):
    service = AccessoriesService(db)
    return service.create_accessory(accessory)


@router.put("/accessories/{accessory_id}", response_model=AccessoryEntity)
def update_accessory(accessory_id: int, accessory: AccessoryEntity, db: Session = Depends(get_db),
                     current_user: User = Depends(verify_token)):
    service = AccessoriesService(db)
    updated_accessory = service.update_accessory(accessory_id, accessory)
    if updated_accessory is None:
        raise HTTPException(status_code=404, detail="Accessory not found")
    return updated_accessory


@router.delete("/accessories/{accessory_id}", response_model=bool)
def delete_accessory(accessory_id: int, db: Session = Depends(get_db), current_user: User = Depends(verify_token)):
    service = AccessoriesService(db)
    success = service.delete_accessory(accessory_id)
    if not success:
        raise HTTPException(status_code=404, detail="Accessory not found")
    return success
