"""Pydantic schemas for user GitLab OAuth + mirror APIs."""
from __future__ import annotations

from datetime import datetime
from typing import Optional
from uuid import UUID

from pydantic import BaseModel, Field


class GitlabConnectUrlResponse(BaseModel):
    connect_url: str
    state: str
    callback_return_url: str
    redirect_uri: str


class GitlabUserConnectionResponse(BaseModel):
    id: UUID
    workspace_id: UUID
    gitlab_user_id: int
    username: str
    name: Optional[str] = None
    base_url: str
    revoked_at: Optional[datetime] = None
    created_at: datetime

    model_config = {"from_attributes": True}


class GitlabProjectItem(BaseModel):
    id: int
    full_path: str
    name: str
    private: bool = False
    default_branch: Optional[str] = None
    web_url: Optional[str] = None


class ProjectGitlabMirrorUpsert(BaseModel):
    workspace_user_gitlab_connection_id: UUID
    target_project_full_path: str = Field(..., min_length=3, max_length=255)
    target_default_branch: str = Field(default="main", max_length=100)
    enabled: bool = True


class ProjectGitlabMirrorResponse(BaseModel):
    id: UUID
    project_id: UUID
    workspace_user_gitlab_connection_id: UUID
    target_project_full_path: str
    target_default_branch: str
    enabled: bool
    last_push_sha: Optional[str] = None
    last_error: Optional[str] = None
    target_project_webhook_id: Optional[str] = None

    model_config = {"from_attributes": True}
