//! OpenAPI documentation configuration using utoipa.

use utoipa::OpenApi;

use crate::db::models::{
    AddLocalRepositoryRequest, AddRemoteRepositoryRequest, BranchInfo, BranchListResponse,
    BranchTaskRequest, BranchTaskResponse,
    ChangePhaseRequest,
    Contract, ContractChatHistoryResponse, ContractChatMessageRecord, ContractEvent,
    ContractListResponse, ContractRepository, ContractSummary, ContractWithRelations,
    CreateContractRequest, CreateDirectiveRequest, CreateDirectiveStepRequest, CreateFileRequest,
    CreateManagedRepositoryRequest, CreateTaskRequest, Daemon, DaemonDirectoriesResponse,
    DaemonDirectory, DaemonListResponse, Directive, DirectiveListResponse, DirectiveStep,
    DirectiveSummary, DirectiveWithSteps,
    File, FileListResponse, FileSummary,
    MergeCommitRequest, MergeCompleteCheckResponse, MergeResolveRequest, MergeResultResponse,
    MergeSkipRequest, MergeStartRequest, MergeStatusResponse, MeshChatConversation,
    MeshChatHistoryResponse, MeshChatMessageRecord, RepositoryHistoryEntry,
    RepositoryHistoryListResponse, RepositorySuggestionsQuery, SendMessageRequest,
    Task,
    TaskEventListResponse, TaskListResponse, TaskSummary, TaskWithSubtasks, TranscriptEntry,
    UpdateContractRequest, UpdateDirectiveRequest, UpdateDirectiveStepRequest,
    UpdateFileRequest, UpdateGoalRequest, UpdateTaskRequest,
};
use crate::server::auth::{
    ApiKey, ApiKeyInfoResponse, CreateApiKeyRequest, CreateApiKeyResponse,
    RefreshApiKeyRequest, RefreshApiKeyResponse, RevokeApiKeyResponse,
};
use crate::server::handlers::{api_keys, contract_chat, contract_discuss, contracts, directives, files, listen, mesh, mesh_chat, mesh_merge, repository_history, users};
use crate::server::messages::{ApiError, AudioEncoding, StartMessage, StopMessage, TranscriptMessage};

#[derive(OpenApi)]
#[openapi(
    info(
        title = "Makima API",
        version = "1.0.0",
        description = "Streaming audio APIs for speech-to-text with persistence.",
        license(name = "MIT"),
    ),
    paths(
        listen::websocket_handler,
        files::list_files,
        files::get_file,
        files::create_file,
        files::update_file,
        files::delete_file,
        files::sync_file_from_repo,
        // Mesh endpoints
        mesh::list_tasks,
        mesh::get_task,
        mesh::create_task,
        mesh::update_task,
        mesh::delete_task,
        mesh::list_subtasks,
        mesh::list_task_events,
        mesh::get_task_output,
        mesh::start_task,
        mesh::stop_task,
        mesh::send_message,
        mesh::retry_completion_action,
        mesh::list_daemons,
        mesh::get_daemon,
        mesh::get_daemon_directories,
        mesh::clone_worktree,
        mesh::check_target_exists,
        mesh::get_task_patch_data,
        mesh::branch_task,
        mesh_chat::get_chat_history,
        mesh_chat::clear_chat_history,
        // Merge endpoints
        mesh_merge::list_branches,
        mesh_merge::merge_start,
        mesh_merge::merge_status,
        mesh_merge::merge_resolve,
        mesh_merge::merge_commit,
        mesh_merge::merge_abort,
        mesh_merge::merge_skip,
        mesh_merge::merge_check,
        // API key endpoints
        api_keys::create_api_key_handler,
        api_keys::get_api_key_handler,
        api_keys::refresh_api_key_handler,
        api_keys::revoke_api_key_handler,
        // User account management endpoints
        users::change_password_handler,
        users::change_email_handler,
        users::delete_account_handler,
        // Contract endpoints
        contracts::list_contracts,
        contracts::get_contract,
        contracts::create_contract,
        contracts::update_contract,
        contracts::delete_contract,
        contracts::change_phase,
        contracts::get_events,
        contracts::add_remote_repository,
        contracts::add_local_repository,
        contracts::create_managed_repository,
        contracts::delete_repository,
        contracts::set_repository_primary,
        contracts::add_task_to_contract,
        contracts::remove_task_from_contract,
        // Contract chat endpoints
        contract_chat::contract_chat_handler,
        contract_chat::get_contract_chat_history,
        contract_chat::clear_contract_chat_history,
        // Contract discuss endpoint
        contract_discuss::discuss_contract_handler,
        // Directive endpoints
        directives::list_directives,
        directives::create_directive,
        directives::get_directive,
        directives::update_directive,
        directives::delete_directive,
        directives::create_step,
        directives::batch_create_steps,
        directives::update_step,
        directives::delete_step,
        directives::start_directive,
        directives::pause_directive,
        directives::advance_directive,
        directives::complete_step,
        directives::fail_step,
        directives::skip_step,
        directives::update_goal,
        // Repository history/settings endpoints
        repository_history::list_repository_history,
        repository_history::get_repository_suggestions,
        repository_history::delete_repository_history,
    ),
    components(
        schemas(
            ApiError,
            AudioEncoding,
            StartMessage,
            StopMessage,
            TranscriptMessage,
            // File schemas
            File,
            FileSummary,
            FileListResponse,
            CreateFileRequest,
            UpdateFileRequest,
            TranscriptEntry,
            // Mesh/Task schemas
            Task,
            TaskSummary,
            TaskListResponse,
            TaskWithSubtasks,
            CreateTaskRequest,
            UpdateTaskRequest,
            SendMessageRequest,
            BranchTaskRequest,
            BranchTaskResponse,
            TaskEventListResponse,
            Daemon,
            DaemonListResponse,
            DaemonDirectoriesResponse,
            DaemonDirectory,
            mesh::TaskPatchDataResponse,
            MeshChatConversation,
            MeshChatMessageRecord,
            MeshChatHistoryResponse,
            // Contract chat schemas
            ContractChatMessageRecord,
            ContractChatHistoryResponse,
            // Contract discuss schemas
            contract_discuss::ChatMessage,
            contract_discuss::DiscussContractRequest,
            contract_discuss::DiscussContractResponse,
            contract_discuss::ToolCallInfo,
            contract_discuss::CreatedContractInfo,
            // Merge schemas
            BranchInfo,
            BranchListResponse,
            MergeStartRequest,
            MergeStatusResponse,
            MergeResolveRequest,
            MergeCommitRequest,
            MergeSkipRequest,
            MergeResultResponse,
            MergeCompleteCheckResponse,
            // API key schemas
            ApiKey,
            ApiKeyInfoResponse,
            CreateApiKeyRequest,
            CreateApiKeyResponse,
            RefreshApiKeyRequest,
            RefreshApiKeyResponse,
            RevokeApiKeyResponse,
            // User account management schemas
            users::ChangePasswordRequest,
            users::ChangePasswordResponse,
            users::ChangeEmailRequest,
            users::ChangeEmailResponse,
            users::DeleteAccountRequest,
            users::DeleteAccountResponse,
            // Contract schemas
            Contract,
            ContractSummary,
            ContractListResponse,
            ContractWithRelations,
            ContractRepository,
            ContractEvent,
            CreateContractRequest,
            UpdateContractRequest,
            AddRemoteRepositoryRequest,
            AddLocalRepositoryRequest,
            CreateManagedRepositoryRequest,
            ChangePhaseRequest,
            // Directive schemas
            Directive,
            DirectiveStep,
            DirectiveWithSteps,
            DirectiveSummary,
            DirectiveListResponse,
            CreateDirectiveRequest,
            UpdateDirectiveRequest,
            UpdateGoalRequest,
            CreateDirectiveStepRequest,
            UpdateDirectiveStepRequest,
            // Repository history schemas
            RepositoryHistoryEntry,
            RepositoryHistoryListResponse,
            RepositorySuggestionsQuery,
        )
    ),
    tags(
        (name = "Listen", description = "Speech-to-text streaming endpoints"),
        (name = "Files", description = "Transcript file management"),
        (name = "Mesh", description = "Task orchestration for Claude Code instances"),
        (name = "Contracts", description = "Contract management with workflow phases"),
        (name = "API Keys", description = "API key management for programmatic access"),
        (name = "Users", description = "User account management"),
        (name = "Directives", description = "Directive management with DAG-based step progression"),
        (name = "Settings", description = "User settings including repository history"),
    )
)]
pub struct ApiDoc;
