"""Pydantic schemas for user Bitbucket 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 BitbucketConnectUrlResponse(BaseModel):
    connect_url: str
    state: str
    callback_return_url: str
    redirect_uri: str


class BitbucketUserConnectionResponse(BaseModel):
    id: UUID
    workspace_id: UUID
    bitbucket_account_id: str
    username: str
    display_name: Optional[str] = None
    revoked_at: Optional[datetime] = None
    created_at: datetime

    model_config = {"from_attributes": True}


class BitbucketRepoItem(BaseModel):
    uuid: str
    full_name: str
    name: str
    private: bool = False
    default_branch: Optional[str] = None
    web_url: Optional[str] = None


class ProjectBitbucketMirrorUpsert(BaseModel):
    workspace_user_bitbucket_connection_id: UUID
    target_repo_full_name: str = Field(..., min_length=3, max_length=255)
    target_default_branch: str = Field(default="main", max_length=100)
    enabled: bool = True


class ProjectBitbucketMirrorResponse(BaseModel):
    id: UUID
    project_id: UUID
    workspace_user_bitbucket_connection_id: UUID
    target_repo_full_name: str
    target_default_branch: str
    enabled: bool
    last_push_sha: Optional[str] = None
    last_error: Optional[str] = None
    target_repo_webhook_uuid: Optional[str] = None

    model_config = {"from_attributes": True}
