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

from core.database import get_core_db
from core.security import get_current_user

from . import service
from .dto import ActivityCreate, ActivityResponse, ActivityUpdate

router = APIRouter()


@router.get("/activities", response_model=list[ActivityResponse])
def list_activities(
    company_id: int = Query(..., description="Company ID"),
    activity_type: str | None = Query(None, description="Filtrar por tipo"),
    activity_status: str | None = Query(None, alias="status", description="Filtrar por estado"),
    account_id: int | None = Query(None),
    contact_id: int | None = Query(None),
    lead_id: int | None = Query(None),
    opportunity_id: int | None = Query(None),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.get_activities(
        db, tenant_id, company_id, activity_type, activity_status,
        account_id, contact_id, lead_id, opportunity_id,
    )


@router.get("/activities/{activity_id}", response_model=ActivityResponse)
def get_activity(
    activity_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.get_activity(db, activity_id, tenant_id, company_id)


@router.post("/activities", response_model=ActivityResponse, status_code=status.HTTP_201_CREATED)
def create_activity(
    payload: ActivityCreate,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.create_activity(db, payload, tenant_id, company_id)


@router.patch("/activities/{activity_id}", response_model=ActivityResponse)
def update_activity(
    activity_id: int,
    payload: ActivityUpdate,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.update_activity(db, activity_id, payload, tenant_id, company_id)


@router.patch("/activities/{activity_id}/complete", response_model=ActivityResponse)
def complete_activity(
    activity_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.complete_activity(db, activity_id, tenant_id, company_id)


@router.delete("/activities/{activity_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_activity(
    activity_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    service.delete_activity(db, activity_id, tenant_id, company_id)
