from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from typing import List
from utils.utils import get_db
from . import handler
from .schema import SubmissionCreate, SubmissionUpdate, SubmissionResponse

router = APIRouter(prefix="/api/submissions", tags=["Submissions"])


@router.post(
    "/",
    response_model=SubmissionResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create New Submission",
    description="Creates a new submission record with user name and email. Validates that name is not empty and between 1-100 characters, and email matches standard email format and is between 5-255 characters. Returns the created submission with auto-generated UUID and timestamps.",
)
def create_submission_route(data: SubmissionCreate, db: Session = Depends(get_db)):
    return handler.create_submission(db, data)


@router.get(
    "/",
    response_model=List[SubmissionResponse],
    summary="List All Submissions",
    description="Returns a paginated list of all submission records ordered by creation date (newest first). Supports optional limit and offset parameters for pagination. All submissions are visible to all users without filtering.",
)
def list_submissions_route(
    limit: int = Query(20, ge=1, le=100),
    offset: int = Query(0, ge=0),
    db: Session = Depends(get_db),
):
    return handler.list_submissions(db, limit, offset)


@router.get(
    "/{submission_id}",
    response_model=SubmissionResponse,
    summary="Get Submission By ID",
    description="Returns a single submission record by its unique identifier. Returns 404 if the submission does not exist.",
)
def get_submission_route(submission_id: str, db: Session = Depends(get_db)):
    return handler.get_submission(db, submission_id)


@router.patch(
    "/{submission_id}",
    response_model=SubmissionResponse,
    summary="Update Submission",
    description="Updates an existing submission record. Accepts partial updates for name and email fields. Validates updated fields according to the same rules as creation. Returns 404 if submission does not exist.",
)
def update_submission_route(
    submission_id: str, data: SubmissionUpdate, db: Session = Depends(get_db)
):
    return handler.update_submission(db, submission_id, data)


@router.delete(
    "/{submission_id}",
    status_code=status.HTTP_204_NO_CONTENT,
    summary="Delete Submission",
    description="Deletes a submission record by its unique identifier. Returns 404 if the submission does not exist. Returns 204 No Content on successful deletion.",
)
def delete_submission_route(submission_id: str, db: Session = Depends(get_db)):
    handler.delete_submission(db, submission_id)