from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, desc
from typing import Optional
from datetime import datetime

from app.database import get_db
from app.models.plan_log import PlanLog
from app.schemas.plan import PlanLogOut
from app.schemas.common import Response, PageResponse
from app.services.auth_service import get_current_user

router = APIRouter()


def _parse_dt(s):
    if not s:
        return None
    try:
        return datetime.fromisoformat(s.replace('Z', ''))
    except Exception:
        return None


@router.get("/", response_model=Response[PageResponse[PlanLogOut]])
async def list_logs(
    page: int = 1,
    page_size: int = 50,
    plan_no: Optional[str] = None,
    action: Optional[str] = None,
    operator: Optional[str] = None,
    start_date: Optional[str] = Query(None),
    end_date: Optional[str] = Query(None),
    db: AsyncSession = Depends(get_db),
    _ = Depends(get_current_user),
):
    query = select(PlanLog).order_by(desc(PlanLog.created_at), desc(PlanLog.id))
    if plan_no:
        query = query.where((PlanLog.plan_no.ilike(f"%{plan_no}%")) | (PlanLog.coil_no.ilike(f"%{plan_no}%")))
    if action:
        query = query.where(PlanLog.action == action)
    if operator:
        query = query.where(PlanLog.operator.ilike(f"%{operator}%"))
    _sd = _parse_dt(start_date)
    if _sd:
        query = query.where(PlanLog.created_at >= _sd)
    _ed = _parse_dt(end_date)
    if _ed:
        query = query.where(PlanLog.created_at <= _ed)

    total = (await db.execute(select(func.count()).select_from(query.subquery()))).scalar()
    result = await db.execute(query.offset((page - 1) * page_size).limit(page_size))
    items = [PlanLogOut.model_validate(x) for x in result.scalars()]
    return Response.ok(PageResponse(total=total, page=page, page_size=page_size, items=items))
