//! 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,
    CleanupResponse,
    CreateContractRequest, CreateDirectiveRequest, CreateDirectiveStepRequest, CreateFileRequest,
    CreateManagedRepositoryRequest, CreateOrderRequest, CreateTaskRequest,
    Daemon, DaemonDirectoriesResponse,
    DaemonDirectory, DaemonListResponse, Directive, DirectiveDocument, DirectiveListResponse,
    DirectiveRevision, DirectiveStep, DirectiveSummary, DirectiveWithSteps,
    File, FileListResponse, FileSummary,
    LinkDirectiveRequest,
    MergeCommitRequest, MergeCompleteCheckResponse, MergeResolveRequest, MergeResultResponse,
    MergeSkipRequest, MergeStartRequest, MergeStatusResponse, MeshChatConversation,
    MeshChatHistoryResponse, MeshChatMessageRecord,
    Order, OrderListResponse, OrderListQuery,
    RepositoryHistoryEntry,
    RepositoryHistoryListResponse, RepositorySuggestionsQuery, SendMessageRequest,
    Task,
    TaskEventListResponse, TaskListResponse, TaskSummary, TaskWithSubtasks, TranscriptEntry,
    UpdateContractRequest, UpdateDirectiveRequest, UpdateDirectiveStepRequest,
    UpdateFileRequest, UpdateGoalRequest, UpdateOrderRequest, UpdateTaskRequest,
};
use crate::server::auth::{
    ApiKey, ApiKeyInfoResponse, CreateApiKeyRequest, CreateApiKeyResponse,
    RefreshApiKeyRequest, RefreshApiKeyResponse, RevokeApiKeyResponse,
};
use crate::server::handlers::{api_keys, directive_documents, directives, files, listen, mesh, mesh_chat, mesh_merge, orders, 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,
        users::get_user_settings_handler,
        users::update_user_settings_handler,
        // Contract endpoints removed in Phase 5.
        // 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,
        directives::list_directive_revisions,
        directives::new_directive_draft,
        directives::create_directive_task,
        directives::list_directive_tasks,
        directives::cleanup_directive,
        directives::create_pr,
        // Contract (directive document) endpoints
        directive_documents::list_documents,
        directive_documents::create_document,
        directive_documents::get_document,
        directive_documents::update_document,
        directive_documents::ship_document,
        directive_documents::archive_document,
        directive_documents::reorder_contract,
        directive_documents::start_contract,
        directive_documents::pause_contract,
        directive_documents::complete_contract,
        directive_documents::unlock_contract,
        directive_documents::list_document_tasks,
        // Order endpoints
        orders::list_orders,
        orders::create_order,
        orders::get_order,
        orders::update_order,
        orders::delete_order,
        orders::link_to_directive,
        orders::convert_to_step,
        // 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 / discuss schemas removed in Phase 5.
            // 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,
            users::UserSettingsResponse,
            users::UpdateUserSettingsRequest,
            // Contract schemas
            Contract,
            ContractSummary,
            ContractListResponse,
            ContractWithRelations,
            ContractRepository,
            ContractEvent,
            CreateContractRequest,
            UpdateContractRequest,
            AddRemoteRepositoryRequest,
            AddLocalRepositoryRequest,
            CreateManagedRepositoryRequest,
            ChangePhaseRequest,
            // Directive schemas
            Directive,
            DirectiveStep,
            DirectiveWithSteps,
            DirectiveSummary,
            DirectiveListResponse,
            DirectiveRevision,
            crate::server::handlers::directives::DirectiveRevisionListResponse,
            crate::server::handlers::directives::CreateDirectiveTaskRequest,
            CreateDirectiveRequest,
            UpdateDirectiveRequest,
            UpdateGoalRequest,
            CreateDirectiveStepRequest,
            UpdateDirectiveStepRequest,
            CleanupResponse,
            // Contract (directive document) schemas
            DirectiveDocument,
            directive_documents::CreateDirectiveDocumentRequest,
            directive_documents::UpdateDirectiveDocumentRequest,
            directive_documents::ShipDirectiveDocumentRequest,
            directive_documents::ReorderDirectiveDocumentRequest,
            directive_documents::CompleteContractRequest,
            directive_documents::DocumentTasksResponse,
            // Order schemas
            Order,
            OrderListResponse,
            OrderListQuery,
            CreateOrderRequest,
            UpdateOrderRequest,
            LinkDirectiveRequest,
            // 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 = "Directive Documents", description = "Directive contracts — multi-document markdown contracts owned by a directive"),
        (name = "Orders", description = "Order management — card-based issue tracking for planned work items"),
        (name = "Settings", description = "User settings including repository history"),
    )
)]
pub struct ApiDoc;
