"""API schemas for dataset processor registrations."""

from __future__ import annotations

import datetime
from uuid import UUID

from app.api.schemas.common import BaseResponse, BaseSchema
from app.core.model import Field


class ProcessorEndpointsSchema(BaseSchema):
    """Routing targets for a registered dataset processor."""

    upload: str | None = Field(
        default=None,
        description="Absolute URL for source-file uploads",
    )
    upload_task_status: str | None = Field(
        default=None,
        description="Template URL for polling upload task status ({task_id})",
    )
    refresh_bucket_commands: str | None = Field(
        default=None,
        description="Template URL for bucket command refresh ({bucket_id})",
    )
    artifacts_download: str | None = Field(
        default=None,
        description="Template URL for artifacts archive download ({alignment_id})",
    )
    validation_reports_download: str | None = Field(
        default=None,
        description="Template URL for validation reports archive download ({alignment_id})",
    )
    retry_failed_buckets: str | None = Field(
        default=None,
        description="Template URL for retrying failed bucket processing ({alignment_id})",
    )


class ProcessorRegistrationBase(BaseSchema):
    """Shared processor registration fields."""

    alignment_type_name: str = Field(
        ...,
        description="Alignment type handled by this processor",
        min_length=1,
        max_length=255,
    )
    processor_name: str = Field(
        ...,
        description="Stable processor identifier",
        min_length=1,
        max_length=255,
    )
    processor_version: str = Field(
        ...,
        description="Deployed processor package version",
        min_length=1,
        max_length=64,
    )
    contract_version: str = Field(
        ...,
        description="alignment-contracts version implemented by this processor",
        min_length=1,
        max_length=32,
    )
    capabilities: list[str] = Field(
        default_factory=list,
        description="Optional capability tags (upload, process, validate, ...)",
    )
    enabled: bool = Field(
        default=True,
        description="Whether routing to this processor is enabled",
    )
    endpoints: ProcessorEndpointsSchema = Field(
        ...,
        description="Routing endpoints for this processor binding",
    )


class ProcessorRegistrationUpsertRequest(ProcessorRegistrationBase):
    """Create or replace the processor binding for an alignment type."""

    pass


class ProcessorRegistrationUpdateRequest(BaseSchema):
    """Partial update for an existing processor registration."""

    processor_name: str | None = Field(default=None, min_length=1, max_length=255)
    processor_version: str | None = Field(default=None, min_length=1, max_length=64)
    contract_version: str | None = Field(default=None, min_length=1, max_length=32)
    capabilities: list[str] | None = None
    enabled: bool | None = None
    endpoints: ProcessorEndpointsSchema | None = None


class ProcessorRegistrationResponse(BaseResponse):
    """Processor registration returned by the API."""

    id: UUID = Field(..., description="Registration identifier")
    alignment_type_id: int = Field(..., description="Bound alignment type identifier")
    alignment_type_name: str = Field(..., description="Bound alignment type name")
    processor_name: str
    processor_version: str
    contract_version: str
    capabilities: list[str]
    enabled: bool
    endpoints: ProcessorEndpointsSchema
    created_at: datetime.datetime
    updated_at: datetime.datetime
