from uuid import UUID
from fastapi import APIRouter, Depends, status, Query
from sqlalchemy.orm import Session

from src.apps.base.schemas.response_model import ResponseModel
from src.apps.base.schemas.locations import (
    LocationCreateSchema,
    LocationUpdateSchema,
    LocationOutSchema,
    LocaleCreateSchema,
    LocaleUpdateSchema,
    LocaleOutSchema,
    LocationFilterSchema,
    LocaleFilterSchema
)
from src.apps.base.services import locations as service
from src.core.dependencies import get_db
from src.utils import constants

router = APIRouter(prefix="/locations", tags=["Locations & Locales"])


# ---------- Locale Routes ----------

@router.get("/locales", response_model=ResponseModel, summary="Get all locales with pagination")
async def get_all_locales(
    page: int = Query(default=1, ge=1),
    per_page: int = Query(default=constants.DEFAULT_PER_PAGE, ge=1, le=constants.MAX_PER_PAGE),
    db: Session = Depends(get_db),
    payload: LocaleFilterSchema = Depends(LocaleFilterSchema)
):
    data = await service.get_all_locales(db, page=page, per_page=per_page, payload=payload)
    return ResponseModel(
        data=data,
        status_code=status.HTTP_200_OK,
        success=True,
        message="Locales fetched successfully"
    )


@router.get("/locales/{locale_id}", response_model=ResponseModel, summary="Get locale by ID")
async def get_locale_by_id(locale_id: int, db: Session = Depends(get_db)):
    locale = await service.get_locale_by_id(db, locale_id)
    return ResponseModel(
        data=LocaleOutSchema.from_orm(locale),
        status_code=status.HTTP_200_OK,
        success=True,
        message="Locale fetched successfully"
    )


@router.post("/locales", response_model=ResponseModel, status_code=status.HTTP_201_CREATED, summary="Create locale for location")
async def create_locale_for_location(payload: LocaleCreateSchema, db: Session = Depends(get_db)):
    created = await service.create_locale(db, payload)
    return ResponseModel(
        data=LocaleOutSchema.from_orm(created),
        status_code=status.HTTP_201_CREATED,
        success=True,
        message="Locale created successfully"
    )


@router.put("/locales/{locale_id}", response_model=ResponseModel, summary="Update locale")
async def update_locale(locale_id: int, payload: LocaleUpdateSchema, db: Session = Depends(get_db)):
    updated = await service.update_locale(db, locale_id, payload)
    return ResponseModel(
        data=LocaleOutSchema.from_orm(updated),
        status_code=status.HTTP_200_OK,
        success=True,
        message="Locale updated successfully"
    )


@router.delete("/locales/{locale_id}", response_model=ResponseModel, summary="Delete locale")
async def delete_locale(locale_id: int, db: Session = Depends(get_db)):
    await service.delete_locale(db, locale_id)
    return ResponseModel(
        data={},
        status_code=200,
        success=True,
        message="Locale deleted successfully"
    )


# ---------- Location Routes ----------

@router.get("", response_model=ResponseModel, summary="Get all locations with pagination")
async def list_locations(
    page: int = Query(default=1, ge=1),
    per_page: int = Query(default=constants.DEFAULT_PER_PAGE, ge=1, le=constants.MAX_PER_PAGE),
    payload: LocationFilterSchema = Depends(),
    db: Session = Depends(get_db),
):
    data = await service.get_all_locations(db, page=page, per_page=per_page, payload=payload)
    return ResponseModel(
        data=data,
        status_code=status.HTTP_200_OK,
        success=True,
        message="Locations fetched successfully"
    )


@router.get("/{location_id}", response_model=ResponseModel, summary="Get location by id")
async def get_location_by_uuid(location_id: int, db: Session = Depends(get_db)):
    location = await service.get_location_by_id(db, location_id)
    return ResponseModel(
        data=LocationOutSchema.from_orm(location),
        status_code=status.HTTP_200_OK,
        success=True,
        message="Location fetched successfully"
    )


@router.post("", response_model=ResponseModel, status_code=status.HTTP_201_CREATED, summary="Create location with locales")
async def create_location_with_locales(payload: LocationCreateSchema, db: Session = Depends(get_db)):
    created = await service.create_location_with_locales(db, payload)
    return ResponseModel(
        data=LocationOutSchema.from_orm(created),
        status_code=status.HTTP_201_CREATED,
        success=True,
        message="Location created successfully"
    )


@router.put("/{location_id}", response_model=ResponseModel, summary="Update location")
async def update_location(location_id: int, payload: LocationUpdateSchema, db: Session = Depends(get_db)):
    updated = await service.update_location(db, location_id, payload)
    return ResponseModel(
        data=LocationOutSchema.from_orm(updated),
        status_code=status.HTTP_200_OK,
        success=True,
        message="Location updated successfully"
    )


@router.delete("/{location_id}", response_model=ResponseModel, summary="Delete location")
async def delete_location(location_id: int, db: Session = Depends(get_db)):
    await service.delete_location(db, location_id)
    return ResponseModel(
        data={},
        status_code=200,
        success=True,
        message="Location deleted successfully"
    )


