from __future__ import annotations

from datetime import datetime
from typing import Annotated
from uuid import UUID

from fastapi import APIRouter, Depends, Query

from v1.schedule_items.dependencies import get_schedule_item_service
from v1.schedule_items.schemas import (
    ScheduleItemCreateRequest,
    ScheduleItemListItem,
    ScheduleItemListRequest,
    ScheduleItemResponse,
    ScheduleItemShareRequest,
    ScheduleItemShareResponse,
    ScheduleItemUpdateRequest,
)
from v1.schedule_items.service import ScheduleItemService


router = APIRouter(prefix="/schedule-items", tags=["schedule-items"])


@router.post("", response_model=ScheduleItemResponse, status_code=201)
async def create_schedule_item(
    request: ScheduleItemCreateRequest,
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
) -> ScheduleItemResponse:
    return await service.create(request)


@router.get("", response_model=list[ScheduleItemListItem])
async def list_schedule_items(
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
    start_at: datetime = Query(..., description="Start date/time for range query"),
    end_at: datetime = Query(..., description="End date/time for range query"),
) -> list[ScheduleItemListItem]:
    request = ScheduleItemListRequest(start_at=start_at, end_at=end_at)
    items = await service.list_by_date_range(request)
    return [ScheduleItemListItem.model_validate(item) for item in items]


@router.get("/{item_id}", response_model=ScheduleItemResponse)
async def get_schedule_item(
    item_id: UUID,
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
) -> ScheduleItemResponse:
    return await service.get_by_id(item_id)


@router.patch("/{item_id}", response_model=ScheduleItemResponse)
async def update_schedule_item(
    item_id: UUID,
    request: ScheduleItemUpdateRequest,
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
) -> ScheduleItemResponse:
    return await service.update(item_id, request)


@router.delete("/{item_id}", status_code=204)
async def delete_schedule_item(
    item_id: UUID,
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
) -> None:
    await service.delete(item_id)


@router.post("/{item_id}/share", response_model=ScheduleItemShareResponse)
async def share_schedule_item(
    item_id: UUID,
    request: ScheduleItemShareRequest,
    service: Annotated[ScheduleItemService, Depends(get_schedule_item_service)],
) -> ScheduleItemShareResponse:
    return await service.share(item_id, request)
