"""Integration schemas."""
from datetime import datetime
from typing import Optional, List, Dict, Any
from pydantic import BaseModel
from app.models.integration import IntegrationType, IntegrationStatus

class IntegrationBase(BaseModel):
    name: str
    type: IntegrationType

class IntegrationCreate(IntegrationBase):
    base_url: Optional[str] = None
    auth_type: str = "api_key"
    api_key: Optional[str] = None
    api_secret: Optional[str] = None
    webhook_url: Optional[str] = None
    callback_url: Optional[str] = None
    auto_analyze: bool = True
    sync_comments: bool = True
    create_prs: bool = True
    repositories: Optional[List[Dict[str, str]]] = None
    config: Optional[Dict[str, Any]] = None

class IntegrationUpdate(BaseModel):
    name: Optional[str] = None
    base_url: Optional[str] = None
    api_key: Optional[str] = None
    api_secret: Optional[str] = None
    callback_url: Optional[str] = None
    auto_analyze: Optional[bool] = None
    sync_comments: Optional[bool] = None
    create_prs: Optional[bool] = None
    repositories: Optional[List[Dict[str, str]]] = None
    status: Optional[IntegrationStatus] = None
    config: Optional[Dict[str, Any]] = None

class IntegrationRead(IntegrationBase):
    id: int
    organization_id: int
    status: IntegrationStatus
    base_url: Optional[str] = None
    webhook_url: Optional[str] = None
    auto_analyze: bool
    issues_processed: Optional[int] = 0
    last_sync_at: Optional[datetime] = None
    last_error: Optional[str] = None
    config: Optional[Dict[str, Any]] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class OAuthCallback(BaseModel):
    code: str
    state: str
