pub use sea_orm_migration::prelude::*;

mod m20260420_000003_add_model_id_to_room_message;
pub mod m20260421_000001_add_agent_type_to_room_ai;
pub mod m20260426_000001_add_thinking_content_to_room_message;

pub async fn execute_sql(manager: &SchemaManager<'_>, sql: &str) -> Result<(), DbErr> {
    for stmt in split_sql_statements(sql) {
        if stmt.is_empty() {
            continue;
        }
        manager
            .get_connection()
            .execute_raw(sea_orm::Statement::from_string(
                sea_orm::DbBackend::Postgres,
                stmt,
            ))
            .await?;
    }
    Ok(())
}

fn split_sql_statements(sql: &str) -> Vec<&str> {
    sql.split(';')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_split_simple() {
        let sql = "SELECT 1; SELECT 2; SELECT 3";
        let stmts = split_sql_statements(sql);
        assert_eq!(stmts, &["SELECT 1", "SELECT 2", "SELECT 3"]);
    }
}

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
        vec![
            // Foundation tables (no dependencies)
            Box::new(m20250628_000002_create_user::Migration),
            Box::new(m20250628_000007_create_user_password::Migration),
            Box::new(m20250628_000005_create_user_email::Migration),
            Box::new(m20250628_000003_create_user_2fa::Migration),
            Box::new(m20250628_000006_create_user_notification::Migration),
            Box::new(m20250628_000009_create_user_preferences::Migration),
            Box::new(m20250628_000008_create_user_password_reset::Migration),
            Box::new(m20250628_000010_create_user_relation::Migration),
            Box::new(m20250628_000011_create_user_ssh_key::Migration),
            Box::new(m20250628_000012_create_user_token::Migration),
            Box::new(m20250628_000004_create_user_activity_log::Migration),
            // Project tables (CREATE first, then ALTER after workspace is created)
            Box::new(m20250628_000013_create_project::Migration),
            Box::new(m20250628_000014_create_project_access_log::Migration),
            Box::new(m20250628_000015_create_project_audit_log::Migration),
            Box::new(m20250628_000016_create_project_billing::Migration),
            Box::new(m20250628_000017_create_project_billing_history::Migration),
            Box::new(m20250628_000018_create_project_follow::Migration),
            Box::new(m20250628_000019_create_project_history_name::Migration),
            Box::new(m20250628_000020_create_project_label::Migration),
            Box::new(m20250628_000021_create_project_like::Migration),
            Box::new(m20250628_000022_create_project_member_invitations::Migration),
            Box::new(m20250628_000023_create_project_member_join_answers::Migration),
            Box::new(m20250628_000024_create_project_member_join_request::Migration),
            Box::new(m20250628_000025_create_project_member_join_settings::Migration),
            Box::new(m20250628_000026_create_project_members::Migration),
            Box::new(m20250628_000027_create_project_watch::Migration),
            // Workspace tables (must come after project tables so ALTER project works)
            Box::new(m20260411_000001_create_workspace::Migration),
            Box::new(m20260411_000002_create_workspace_membership::Migration),
            Box::new(m20260411_000003_add_workspace_id_to_project::Migration),
            Box::new(m20260411_000004_add_invite_token_to_workspace_membership::Migration),
            Box::new(m20260412_000001_create_workspace_billing::Migration),
            Box::new(m20260412_000002_create_workspace_billing_history::Migration),
            Box::new(m20260412_000003_create_project_skill::Migration),
            Box::new(m20260413_000001_add_skill_commit_blob::Migration),
            Box::new(m20260414_000001_create_agent_task::Migration),
            Box::new(m20260415_000001_add_issue_id_to_agent_task::Migration),
            Box::new(m20260416_000001_add_retry_count_to_agent_task::Migration),
            Box::new(m20260417_000001_add_stream_to_room_ai::Migration),
            Box::new(m20260420_000001_create_room_attachment::Migration),
            Box::new(m20260420_000002_add_push_subscription::Migration),
            Box::new(m20260420_000003_add_model_id_to_room_message::Migration),
            Box::new(m20260421_000001_add_agent_type_to_room_ai::Migration),
            Box::new(m20260426_000001_add_thinking_content_to_room_message::Migration),
            // Repo tables
            Box::new(m20250628_000028_create_repo::Migration),
            Box::new(m20250628_000029_create_repo_branch::Migration),
            Box::new(m20250628_000030_create_repo_branch_protect::Migration),
            Box::new(m20250628_000031_create_repo_collaborator::Migration),
            Box::new(m20250628_000032_create_repo_commit::Migration),
            Box::new(m20250628_000033_create_repo_fork::Migration),
            Box::new(m20250628_000034_create_repo_history_name::Migration),
            Box::new(m20250628_000035_create_repo_hook::Migration),
            Box::new(m20250628_000036_create_repo_lfs_lock::Migration),
            Box::new(m20250628_000037_create_repo_lfs_object::Migration),
            Box::new(m20250628_000038_create_repo_lock::Migration),
            Box::new(m20250628_000039_create_repo_star::Migration),
            Box::new(m20250628_000040_create_repo_tag::Migration),
            Box::new(m20250628_000041_create_repo_upstream::Migration),
            Box::new(m20250628_000042_create_repo_watch::Migration),
            Box::new(m20250628_000043_create_repo_webhook::Migration),
            // Issue tables
            Box::new(m20250628_000044_create_issue::Migration),
            Box::new(m20250628_000045_create_issue_assignee::Migration),
            Box::new(m20250628_000046_create_issue_comment::Migration),
            Box::new(m20250628_000047_create_issue_comment_reaction::Migration),
            Box::new(m20250628_000048_create_issue_label::Migration),
            Box::new(m20250628_000049_create_issue_pull_request::Migration),
            Box::new(m20250628_000050_create_issue_reaction::Migration),
            Box::new(m20250628_000051_create_issue_repo::Migration),
            Box::new(m20250628_000052_create_issue_subscriber::Migration),
            // Pull request tables
            Box::new(m20250628_000053_create_pull_request::Migration),
            Box::new(m20250628_000054_create_pull_request_commit::Migration),
            Box::new(m20250628_000055_create_pull_request_review::Migration),
            Box::new(m20250628_000056_create_pull_request_review_comment::Migration),
            // Room tables
            Box::new(m20250628_000057_create_room_category::Migration),
            Box::new(m20250628_000058_create_room::Migration),
            Box::new(m20250628_000059_create_room_ai::Migration),
            Box::new(m20250628_000060_create_room_member::Migration),
            Box::new(m20250628_000061_create_room_message::Migration),
            Box::new(m20250628_000062_create_room_pin::Migration),
            Box::new(m20250628_000063_create_room_thread::Migration),
            // Agent tables
            Box::new(m20250628_000068_create_ai_model_provider::Migration),
            Box::new(m20250628_000064_create_ai_model::Migration),
            Box::new(m20250628_000069_create_ai_model_version::Migration),
            Box::new(m20250628_000065_create_ai_model_capability::Migration),
            Box::new(m20250628_000066_create_ai_model_parameter_profile::Migration),
            Box::new(m20250628_000067_create_ai_model_pricing::Migration),
            // AI session tables
            Box::new(m20250628_000070_create_ai_session::Migration),
            Box::new(m20250628_000072_create_ai_tool_call::Migration),
            Box::new(m20250628_000071_create_ai_tool_auth::Migration),
            // System tables
            Box::new(m20250628_000073_create_label::Migration),
            Box::new(m20250628_000074_create_notify::Migration),
            Box::new(m20250628_000076_create_user_email_change::Migration),
            Box::new(m20250628_000077_create_project_activity::Migration),
            Box::new(m20250628_000078_add_room_member_do_not_disturb::Migration),
            Box::new(m20250628_000079_add_room_message_in_reply_to::Migration),
            Box::new(m20250628_000080_add_message_reactions_and_search::Migration),
            Box::new(m20250628_000081_add_message_edit_history::Migration),
            Box::new(m20250628_000082_add_pr_review_comment_resolve::Migration),
            Box::new(m20250628_000083_add_pr_review_request::Migration),
            Box::new(m20260407_000001_extend_repo_branch_protect::Migration),
            Box::new(m20260407_000002_create_project_board::Migration),
            Box::new(m20260407_000003_add_repo_ai_code_review::Migration),
            // All CREATE TABLE migrations now use correct column types (TIMESTAMPTZ) and names.
            // For existing databases that were created with buggy migrations, apply the fix
            // by manually running: ALTER TABLE ... ALTER COLUMN ... TYPE TIMESTAMPTZ;
            // Room notifications (already existed)
            Box::new(m20250628_000001_create_room_notifications::Migration),
        ]
    }
}

pub mod m20250628_000001_create_room_notifications;
pub mod m20250628_000002_create_user;
pub mod m20250628_000003_create_user_2fa;
pub mod m20250628_000004_create_user_activity_log;
pub mod m20250628_000005_create_user_email;
pub mod m20250628_000006_create_user_notification;
pub mod m20250628_000007_create_user_password;
pub mod m20250628_000008_create_user_password_reset;
pub mod m20250628_000009_create_user_preferences;
pub mod m20250628_000010_create_user_relation;
pub mod m20250628_000011_create_user_ssh_key;
pub mod m20250628_000012_create_user_token;
pub mod m20250628_000013_create_project;
pub mod m20250628_000014_create_project_access_log;
pub mod m20250628_000015_create_project_audit_log;
pub mod m20250628_000016_create_project_billing;
pub mod m20250628_000017_create_project_billing_history;
pub mod m20250628_000018_create_project_follow;
pub mod m20250628_000019_create_project_history_name;
pub mod m20250628_000020_create_project_label;
pub mod m20250628_000021_create_project_like;
pub mod m20250628_000022_create_project_member_invitations;
pub mod m20250628_000023_create_project_member_join_answers;
pub mod m20250628_000024_create_project_member_join_request;
pub mod m20250628_000025_create_project_member_join_settings;
pub mod m20250628_000026_create_project_members;
pub mod m20250628_000027_create_project_watch;
pub mod m20250628_000028_create_repo;
pub mod m20250628_000029_create_repo_branch;
pub mod m20250628_000030_create_repo_branch_protect;
pub mod m20250628_000031_create_repo_collaborator;
pub mod m20250628_000032_create_repo_commit;
pub mod m20250628_000033_create_repo_fork;
pub mod m20250628_000034_create_repo_history_name;
pub mod m20250628_000035_create_repo_hook;
pub mod m20250628_000036_create_repo_lfs_lock;
pub mod m20250628_000037_create_repo_lfs_object;
pub mod m20250628_000038_create_repo_lock;
pub mod m20250628_000039_create_repo_star;
pub mod m20250628_000040_create_repo_tag;
pub mod m20250628_000041_create_repo_upstream;
pub mod m20250628_000042_create_repo_watch;
pub mod m20250628_000043_create_repo_webhook;
pub mod m20250628_000044_create_issue;
pub mod m20250628_000045_create_issue_assignee;
pub mod m20250628_000046_create_issue_comment;
pub mod m20250628_000047_create_issue_comment_reaction;
pub mod m20250628_000048_create_issue_label;
pub mod m20250628_000049_create_issue_pull_request;
pub mod m20250628_000050_create_issue_reaction;
pub mod m20250628_000051_create_issue_repo;
pub mod m20250628_000052_create_issue_subscriber;
pub mod m20250628_000053_create_pull_request;
pub mod m20250628_000054_create_pull_request_commit;
pub mod m20250628_000055_create_pull_request_review;
pub mod m20250628_000056_create_pull_request_review_comment;
pub mod m20250628_000057_create_room_category;
pub mod m20250628_000058_create_room;
pub mod m20250628_000059_create_room_ai;
pub mod m20250628_000060_create_room_member;
pub mod m20250628_000061_create_room_message;
pub mod m20250628_000062_create_room_pin;
pub mod m20250628_000063_create_room_thread;
pub mod m20250628_000064_create_ai_model;
pub mod m20250628_000065_create_ai_model_capability;
pub mod m20250628_000066_create_ai_model_parameter_profile;
pub mod m20250628_000067_create_ai_model_pricing;
pub mod m20250628_000068_create_ai_model_provider;
pub mod m20250628_000069_create_ai_model_version;
pub mod m20250628_000070_create_ai_session;
pub mod m20250628_000071_create_ai_tool_auth;
pub mod m20250628_000072_create_ai_tool_call;
pub mod m20250628_000073_create_label;
pub mod m20250628_000074_create_notify;
pub mod m20250628_000075_fix_column_types_and_names;
pub mod m20250628_000076_create_user_email_change;
pub mod m20250628_000077_create_project_activity;
pub mod m20250628_000078_add_room_member_do_not_disturb;
pub mod m20250628_000079_add_room_message_in_reply_to;
pub mod m20250628_000080_add_message_reactions_and_search;
pub mod m20250628_000081_add_message_edit_history;
pub mod m20250628_000082_add_pr_review_comment_resolve;
pub mod m20250628_000083_add_pr_review_request;
pub mod m20260407_000001_extend_repo_branch_protect;
pub mod m20260407_000002_create_project_board;
pub mod m20260407_000003_add_repo_ai_code_review;
pub mod m20260411_000001_create_workspace;
pub mod m20260411_000002_create_workspace_membership;
pub mod m20260411_000003_add_workspace_id_to_project;
pub mod m20260411_000004_add_invite_token_to_workspace_membership;
pub mod m20260412_000001_create_workspace_billing;
pub mod m20260412_000002_create_workspace_billing_history;
pub mod m20260412_000003_create_project_skill;
pub mod m20260413_000001_add_skill_commit_blob;
pub mod m20260414_000001_create_agent_task;
pub mod m20260415_000001_add_issue_id_to_agent_task;
pub mod m20260416_000001_add_retry_count_to_agent_task;
pub mod m20260417_000001_add_stream_to_room_ai;
pub mod m20260420_000001_create_room_attachment;
pub mod m20260420_000002_add_push_subscription;
