
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from core.database import get_core_db
from core.security import get_current_user
from .service import CanadianTariffFractionService
from .schemas import (
    CanadianTariffFractionResponse,
    CanadianTariffFractionCreate,
    CanadianTariffFractionUpdate,
    CanadianTariffFractionListResponse
)

from api.v1.common.tenant_crud_routes import validate_access_to_resource

router = APIRouter()

@router.get("/", response_model=CanadianTariffFractionListResponse)
def list_canadian_fractions(
    company_id: int = Query(..., description="Company ID"),
    page: int = Query(1, ge=1),
    page_size: int = Query(50, ge=1, le=1000),
    search: Optional[str] = None,
    db: Session = Depends(get_core_db),
    current_user = Depends(get_current_user)
):
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["frac_canadian.view"]
    )
    skip = (page - 1) * page_size
    service = CanadianTariffFractionService(db)
    items, total = service.get_multi(
        tenant_id=tenant_id, 
        company_id=company_id, 
        skip=skip, 
        limit=page_size, 
        search=search
    )
    return {
        "items": items,
        "total": total,
        "page": page,
        "page_size": page_size,
        "pages": (total + page_size - 1) // page_size if page_size > 0 else 1
    }

@router.get("/{id}", response_model=CanadianTariffFractionResponse)
def get_canadian_fraction(
    id: int,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user = Depends(get_current_user)
):
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["frac_canadian.view"]
    )
    service = CanadianTariffFractionService(db)
    item = service.get(id, tenant_id, company_id)
    if not item:
        raise HTTPException(status_code=404, detail="Not found")
    return item

@router.post("/", response_model=CanadianTariffFractionResponse)
def create_canadian_fraction(
    item_in: CanadianTariffFractionCreate,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user = Depends(get_current_user)
):
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["frac_canadian.create"]
    )
    service = CanadianTariffFractionService(db)
    return service.create(item_in, tenant_id, company_id)

@router.put("/{id}", response_model=CanadianTariffFractionResponse)
def update_canadian_fraction(
    id: int,
    item_in: CanadianTariffFractionUpdate,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user = Depends(get_current_user)
):
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["frac_canadian.edit"]
    )
    service = CanadianTariffFractionService(db)
    item = service.get(id, tenant_id, company_id)
    if not item:
        raise HTTPException(status_code=404, detail="Not found")
    return service.update(item, item_in)

@router.delete("/{id}", response_model=CanadianTariffFractionResponse)
def delete_canadian_fraction(
    id: int,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user = Depends(get_current_user)
):
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["frac_canadian.delete"]
    )
    service = CanadianTariffFractionService(db)
    item = service.get(id, tenant_id, company_id)
    if not item:
        raise HTTPException(status_code=404, detail="Not found")
    return service.delete(id, tenant_id, company_id)
