"""
Report Templates Router

CRUD operations for report template management.
Templates store time filter presets and report configuration for reuse.
"""

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from datetime import datetime
from typing import Optional
import uuid

from backend.database import get_db
from backend.models import ReportTemplate

router = APIRouter(prefix="/api/report-templates", tags=["report-templates"])


@router.get("")
async def list_templates(
    project_id: Optional[str] = None,
    db: Session = Depends(get_db),
):
    """
    List all report templates.
    Optionally filter by project_id (includes global templates with project_id=None).
    """
    query = db.query(ReportTemplate)

    if project_id:
        # Include global templates (project_id=None) AND project-specific templates
        query = query.filter(
            (ReportTemplate.project_id == None) | (ReportTemplate.project_id == project_id)
        )

    templates = query.order_by(ReportTemplate.name).all()

    return [
        {
            "id": t.id,
            "name": t.name,
            "project_id": t.project_id,
            "report_title": t.report_title,
            "start_time": t.start_time,
            "end_time": t.end_time,
            "start_date": t.start_date,
            "end_date": t.end_date,
            "created_at": t.created_at.isoformat() if t.created_at else None,
            "updated_at": t.updated_at.isoformat() if t.updated_at else None,
        }
        for t in templates
    ]


@router.post("")
async def create_template(
    data: dict,
    db: Session = Depends(get_db),
):
    """
    Create a new report template.

    Request body:
    - name: Template name (required)
    - project_id: Optional project ID for project-specific template
    - report_title: Default report title
    - start_time: Start time filter (HH:MM format)
    - end_time: End time filter (HH:MM format)
    - start_date: Start date filter (YYYY-MM-DD format)
    - end_date: End date filter (YYYY-MM-DD format)
    """
    name = data.get("name")
    if not name:
        raise HTTPException(status_code=400, detail="Template name is required")

    template = ReportTemplate(
        id=str(uuid.uuid4()),
        name=name,
        project_id=data.get("project_id"),
        report_title=data.get("report_title", "Background Noise Study"),
        start_time=data.get("start_time"),
        end_time=data.get("end_time"),
        start_date=data.get("start_date"),
        end_date=data.get("end_date"),
    )

    db.add(template)
    db.commit()
    db.refresh(template)

    return {
        "id": template.id,
        "name": template.name,
        "project_id": template.project_id,
        "report_title": template.report_title,
        "start_time": template.start_time,
        "end_time": template.end_time,
        "start_date": template.start_date,
        "end_date": template.end_date,
        "created_at": template.created_at.isoformat() if template.created_at else None,
    }


@router.get("/{template_id}")
async def get_template(
    template_id: str,
    db: Session = Depends(get_db),
):
    """Get a specific report template by ID."""
    template = db.query(ReportTemplate).filter_by(id=template_id).first()
    if not template:
        raise HTTPException(status_code=404, detail="Template not found")

    return {
        "id": template.id,
        "name": template.name,
        "project_id": template.project_id,
        "report_title": template.report_title,
        "start_time": template.start_time,
        "end_time": template.end_time,
        "start_date": template.start_date,
        "end_date": template.end_date,
        "created_at": template.created_at.isoformat() if template.created_at else None,
        "updated_at": template.updated_at.isoformat() if template.updated_at else None,
    }


@router.put("/{template_id}")
async def update_template(
    template_id: str,
    data: dict,
    db: Session = Depends(get_db),
):
    """Update an existing report template."""
    template = db.query(ReportTemplate).filter_by(id=template_id).first()
    if not template:
        raise HTTPException(status_code=404, detail="Template not found")

    # Update fields if provided
    if "name" in data:
        template.name = data["name"]
    if "project_id" in data:
        template.project_id = data["project_id"]
    if "report_title" in data:
        template.report_title = data["report_title"]
    if "start_time" in data:
        template.start_time = data["start_time"]
    if "end_time" in data:
        template.end_time = data["end_time"]
    if "start_date" in data:
        template.start_date = data["start_date"]
    if "end_date" in data:
        template.end_date = data["end_date"]

    template.updated_at = datetime.utcnow()
    db.commit()
    db.refresh(template)

    return {
        "id": template.id,
        "name": template.name,
        "project_id": template.project_id,
        "report_title": template.report_title,
        "start_time": template.start_time,
        "end_time": template.end_time,
        "start_date": template.start_date,
        "end_date": template.end_date,
        "updated_at": template.updated_at.isoformat() if template.updated_at else None,
    }


@router.delete("/{template_id}")
async def delete_template(
    template_id: str,
    db: Session = Depends(get_db),
):
    """Delete a report template."""
    template = db.query(ReportTemplate).filter_by(id=template_id).first()
    if not template:
        raise HTTPException(status_code=404, detail="Template not found")

    db.delete(template)
    db.commit()

    return JSONResponse({"status": "success", "message": "Template deleted"})
