from typing import Optional
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from src.core.database import get_db
from src.apps.auth.utils.auth import get_current_merchant
from src.apps.settings import services
from src.apps.settings.schemas.account import (
    AccountSummaryResponse,
    ContactCreate,
    ContactUpdate,
    ContactResponse,
    LocationCreate,
    LocationUpdate,
    LocationResponse,
)

router = APIRouter()


@router.get("/summary", response_model=AccountSummaryResponse)
async def get_account_summary(
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_account_summary(current_merchant.id, db)


@router.get("/contacts")
async def list_contacts(
    page: int = Query(1, ge=1),
    per_page: int = Query(10, ge=1, le=100),
    search: Optional[str] = Query(None),
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    result = services.list_contacts(current_merchant.id, db, page=page, per_page=per_page, search=search)
    return {
        "data": [ContactResponse.model_validate(c) for c in result["data"]],
        "meta": result["meta"],
    }


@router.post("/contacts", response_model=ContactResponse)
async def create_contact(
    data: ContactCreate,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    contact = services.create_contact(
        current_merchant.id, data.model_dump(exclude_none=True), db
    )
    db.commit()
    db.refresh(contact)
    return contact


@router.put("/contacts/{contact_id}", response_model=ContactResponse)
async def update_contact(
    contact_id: int,
    data: ContactUpdate,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    contact = services.update_contact(
        contact_id, current_merchant.id, data.model_dump(exclude_none=True), db
    )
    db.commit()
    db.refresh(contact)
    return contact


@router.delete("/contacts/{contact_id}", status_code=204)
async def delete_contact(
    contact_id: int,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    services.delete_contact(contact_id, current_merchant.id, db)
    db.commit()


@router.get("/locations")
async def list_locations(
    page: int = Query(1, ge=1),
    per_page: int = Query(10, ge=1, le=100),
    search: Optional[str] = Query(None),
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    result = services.list_locations(current_merchant.id, db, page=page, per_page=per_page, search=search)
    return {
        "data": [LocationResponse.model_validate(loc) for loc in result["data"]],
        "meta": result["meta"],
    }


@router.post("/locations", response_model=LocationResponse)
async def create_location(
    data: LocationCreate,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    location = services.create_location(
        current_merchant.id, data.model_dump(exclude_none=True), db
    )
    db.commit()
    db.refresh(location)
    return location


@router.put("/locations/{location_id}", response_model=LocationResponse)
async def update_location(
    location_id: int,
    data: LocationUpdate,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    location = services.update_location(
        location_id, current_merchant.id, data.model_dump(exclude_none=True), db
    )
    db.commit()
    db.refresh(location)
    return location


@router.delete("/locations/{location_id}", status_code=204)
async def delete_location(
    location_id: int,
    current_merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    services.delete_location(location_id, current_merchant.id, db)
    db.commit()
