from datetime import datetime
from typing import Any, Dict, List, Optional

from pydantic import BaseModel


class ToolDefinitionResponse(BaseModel):
    name: str
    version: str
    description: str
    input_schema: Dict[str, Any]
    output_schema: Dict[str, Any]


class SRSToolCreateJobResponse(BaseModel):
    job_id: int
    status: str


class SRSToolJobStatusResponse(BaseModel):
    job_id: int
    tool_name: str
    status: str
    error_message: Optional[str] = None
    extraction_id: Optional[int] = None
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None


class SRSToolRequirementItem(BaseModel):
    id: str
    description: str
    priority: str
    acceptanceCriteria: List[str]
    sourceField: str
    sectionUid: Optional[str] = None
    sectionNumber: Optional[str] = None
    sectionTitle: Optional[str] = None
    requirementType: Optional[str] = None
    interfaceName: Optional[str] = None
    interfaceType: Optional[str] = None
    dataSource: Optional[str] = None
    dataDestination: Optional[str] = None
    sortOrder: int


class SRSToolResultResponse(BaseModel):
    jobId: int
    documentName: str
    generatedAt: str
    statistics: Dict[str, Any]
    requirements: List[SRSToolRequirementItem]
    rawOutput: Dict[str, Any]


class SRSToolHistoryItem(BaseModel):
    jobId: int
    documentName: str
    generatedAt: str
    totalRequirements: int
    status: str
    createdAt: str
    updatedAt: str


class SRSToolRequirementsSaveRequest(BaseModel):
    requirements: List[SRSToolRequirementItem]


class TestingGenerationSaveRequest(BaseModel):
    source_job_id: Optional[int] = None
    source_document_name: str
    knowledge_base_id: Optional[int] = None
    generated_file: Dict[str, Any]


class TestingGenerationCreateRequest(BaseModel):
    source_job_id: Optional[int] = None
    source_document_name: str
    knowledge_base_id: Optional[int] = None
    requirements: List[SRSToolRequirementItem]


class TestingGenerationCreateResponse(BaseModel):
    job_id: int
    status: str


class TestingGenerationJobStatusResponse(BaseModel):
    job_id: int
    tool_name: str
    status: str
    error_message: Optional[str] = None
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    source_document_name: Optional[str] = None
    current_step: Optional[int] = None
    total_steps: Optional[int] = None
    current_requirement_id: Optional[str] = None


class TestingGenerationResultResponse(BaseModel):
    jobId: int
    sourceJobId: Optional[int] = None
    sourceDocumentName: str
    generatedAt: str
    totalRequirements: int
    knowledgeBaseId: Optional[int] = None
    generatedFile: Dict[str, Any]


class TestingGenerationHistoryItem(BaseModel):
    jobId: int
    sourceJobId: Optional[int] = None
    sourceDocumentName: str
    generatedAt: str
    totalRequirements: int
    knowledgeBaseId: Optional[int] = None
    status: str
    createdAt: str
    updatedAt: str
