"""add_ci_fields

Revision ID: ecc7c0ff0957
Revises: 008_add_files_with_findings
Create Date: 2025-12-31 15:26:51.588929

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'ecc7c0ff0957'
down_revision = '008_add_files_with_findings'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('pr_reviews',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('project_id', sa.String(), nullable=False),
    sa.Column('pr_number', sa.Integer(), nullable=False),
    sa.Column('commit_sha', sa.String(), nullable=True),
    sa.Column('event_type', sa.String(), nullable=False),
    sa.Column('summary', sa.Text(), nullable=True),
    sa.Column('full_report', sa.Text(), nullable=True),
    sa.Column('context_used', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.drop_index(op.f('ix_agent_tasks_created_at'), table_name='agent_tasks')
    op.drop_index(op.f('ix_agent_tasks_created_by'), table_name='agent_tasks')
    op.drop_index(op.f('ix_agent_tasks_project_id'), table_name='agent_tasks')
    op.drop_index(op.f('ix_agent_tasks_status'), table_name='agent_tasks')
    op.alter_column('audit_issues', 'status',
               existing_type=sa.VARCHAR(),
               nullable=True,
               existing_server_default=sa.text("'open'::character varying"))
    op.drop_index(op.f('ix_audit_rule_sets_language'), table_name='audit_rule_sets')
    op.drop_index(op.f('ix_audit_rule_sets_rule_type'), table_name='audit_rule_sets')
    op.drop_index(op.f('ix_audit_rules_category'), table_name='audit_rules')
    op.drop_index(op.f('ix_audit_rules_rule_code'), table_name='audit_rules')
    op.alter_column('audit_tasks', 'status',
               existing_type=sa.VARCHAR(),
               nullable=True,
               existing_server_default=sa.text("'pending'::character varying"))
    op.alter_column('audit_tasks', 'total_files',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'scanned_files',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'total_lines',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'issues_count',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'quality_score',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=True,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('instant_analyses', 'issues_count',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('instant_analyses', 'quality_score',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=True,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('instant_analyses', 'analysis_time',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=True,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('project_members', 'role',
               existing_type=sa.VARCHAR(),
               nullable=True,
               existing_server_default=sa.text("'member'::character varying"))
    op.add_column('projects', sa.Column('is_ci_managed', sa.Boolean(), nullable=False, server_default=sa.text('false')))
    op.add_column('projects', sa.Column('latest_pr_activity', sa.DateTime(timezone=True), nullable=True))
    op.alter_column('projects', 'source_type',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text("'repository'::character varying"))
    op.alter_column('projects', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('true'))
    op.drop_index(op.f('ix_projects_owner_id'), table_name='projects')
    op.create_index(op.f('ix_projects_name'), 'projects', ['name'], unique=False)
    op.drop_column('projects', 'is_deleted')
    op.drop_column('projects', 'status')
    op.drop_column('projects', 'deleted_at')
    op.drop_index(op.f('ix_prompt_templates_is_system'), table_name='prompt_templates')
    op.drop_index(op.f('ix_prompt_templates_template_type'), table_name='prompt_templates')
    op.alter_column('users', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('true'))
    op.alter_column('users', 'is_superuser',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('false'))
    op.alter_column('users', 'role',
               existing_type=sa.VARCHAR(),
               nullable=True,
               existing_server_default=sa.text("'user'::character varying"))
    op.drop_constraint(op.f('users_email_key'), 'users', type_='unique')
    op.create_index(op.f('ix_users_full_name'), 'users', ['full_name'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_users_full_name'), table_name='users')
    op.create_unique_constraint(op.f('users_email_key'), 'users', ['email'], postgresql_nulls_not_distinct=False)
    op.alter_column('users', 'role',
               existing_type=sa.VARCHAR(),
               nullable=False,
               existing_server_default=sa.text("'user'::character varying"))
    op.alter_column('users', 'is_superuser',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('false'))
    op.alter_column('users', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('true'))
    op.create_index(op.f('ix_prompt_templates_template_type'), 'prompt_templates', ['template_type'], unique=False)
    op.create_index(op.f('ix_prompt_templates_is_system'), 'prompt_templates', ['is_system'], unique=False)
    op.add_column('projects', sa.Column('deleted_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True))
    op.add_column('projects', sa.Column('status', sa.VARCHAR(), server_default=sa.text("'active'::character varying"), autoincrement=False, nullable=False))
    op.add_column('projects', sa.Column('is_deleted', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False))
    op.drop_index(op.f('ix_projects_name'), table_name='projects')
    op.create_index(op.f('ix_projects_owner_id'), 'projects', ['owner_id'], unique=False)
    op.alter_column('projects', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('true'))
    op.alter_column('projects', 'source_type',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text("'repository'::character varying"))
    op.drop_column('projects', 'latest_pr_activity')
    op.drop_column('projects', 'is_ci_managed')
    op.alter_column('project_members', 'role',
               existing_type=sa.VARCHAR(),
               nullable=False,
               existing_server_default=sa.text("'member'::character varying"))
    op.alter_column('instant_analyses', 'analysis_time',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=False,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('instant_analyses', 'quality_score',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=False,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('instant_analyses', 'issues_count',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'quality_score',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               nullable=False,
               existing_server_default=sa.text("'0'::double precision"))
    op.alter_column('audit_tasks', 'issues_count',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'total_lines',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'scanned_files',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'total_files',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('audit_tasks', 'status',
               existing_type=sa.VARCHAR(),
               nullable=False,
               existing_server_default=sa.text("'pending'::character varying"))
    op.create_index(op.f('ix_audit_rules_rule_code'), 'audit_rules', ['rule_code'], unique=False)
    op.create_index(op.f('ix_audit_rules_category'), 'audit_rules', ['category'], unique=False)
    op.create_index(op.f('ix_audit_rule_sets_rule_type'), 'audit_rule_sets', ['rule_type'], unique=False)
    op.create_index(op.f('ix_audit_rule_sets_language'), 'audit_rule_sets', ['language'], unique=False)
    op.alter_column('audit_issues', 'status',
               existing_type=sa.VARCHAR(),
               nullable=False,
               existing_server_default=sa.text("'open'::character varying"))
    op.create_index(op.f('ix_agent_tasks_status'), 'agent_tasks', ['status'], unique=False)
    op.create_index(op.f('ix_agent_tasks_project_id'), 'agent_tasks', ['project_id'], unique=False)
    op.create_index(op.f('ix_agent_tasks_created_by'), 'agent_tasks', ['created_by'], unique=False)
    op.create_index(op.f('ix_agent_tasks_created_at'), 'agent_tasks', ['created_at'], unique=False)
    op.drop_table('pr_reviews')
    # ### end Alembic commands ###






