from __future__ import annotations

from datetime import datetime
from typing import Any

from app.schemas.common import ApiModel


class EditorChangeRead(ApiModel):
    id: str
    variant_id: str
    change_type: str
    selector_type: str
    selector_value: str
    sort_order: int
    payload: dict[str, Any] | list[Any] | None = None


class EditorChangeWrite(ApiModel):
    id: str | None = None
    change_type: str
    selector_type: str = "css"
    selector_value: str
    sort_order: int = 0
    payload: dict[str, Any] | list[Any] | None = None


class EditorChangeListResponse(ApiModel):
    items: list[EditorChangeRead]


class SaveVariantChangesRequest(ApiModel):
    items: list[EditorChangeWrite]


class EditorSessionCreateRequest(ApiModel):
    variant_id: str
    base_url: str
    mode: str = "edit"


class EditorSessionUpdateRequest(ApiModel):
    status: str | None = None
    draft_changes: list[dict[str, Any]] | None = None


class EditorSessionRead(ApiModel):
    id: str
    variant_id: str
    mode: str
    base_url: str
    actor_id: str
    actor_email: str | None = None
    status: str
    draft_changes: list[dict[str, Any]]
    created_at: datetime
    updated_at: datetime


class BuildPreviewRequest(ApiModel):
    variant_id: str
    items: list[EditorChangeWrite]


class PreviewOperationRead(ApiModel):
    selector_type: str
    selector_value: str
    action: str
    payload: dict[str, Any] | list[Any] | None = None


class BuildPreviewResponse(ApiModel):
    variant_id: str
    generated_at: datetime
    operations: list[PreviewOperationRead]

