"""Schemas for Forgejo issue operations."""

from __future__ import annotations

from datetime import datetime
from typing import Any
from uuid import UUID

from sqlmodel import SQLModel


class ForgejoIssueBase(SQLModel):
    """Shared issue fields used across create/read payloads."""

    forgejo_issue_number: int
    title: str
    body: str | None = None
    body_preview: str | None = None
    state: str
    is_pull_request: bool
    labels: list[dict[str, Any]] = []
    assignees: list[dict[str, Any]] = []
    milestone: dict[str, Any] | None = None
    author: str
    html_url: str
    forgejo_created_at: datetime
    forgejo_updated_at: datetime
    forgejo_closed_at: datetime | None = None


class ForgejoIssueCreate(ForgejoIssueBase):
    """Payload for creating a Forgejo issue record."""


class ForgejoIssueRead(ForgejoIssueBase):
    """Issue payload returned from read endpoints."""

    id: UUID
    organization_id: UUID
    repository_id: UUID
    created_at: datetime
    updated_at: datetime


class ForgejoIssueListResponse(SQLModel):
    """Paginated list response for issues."""

    items: list[ForgejoIssueRead]
    total: int
    page: int
    limit: int


class ForgejoIssueUpsertResponse(SQLModel):
    """Response for issue sync operations."""

    created: int = 0
    updated: int = 0
    open: int = 0
    closed: int = 0
    total: int = 0


class CloseIssueResponse(SQLModel):
    """Response for issue close operations."""

    success: bool
    issue_id: UUID
    forgejo_issue_number: int
    state: str
    forgejo_closed_at: str | None = None
    last_synced_at: str
