using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ColaFlow.Modules.Identity.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class FixUserTenantRolesIgnoreNavigation : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // IDEMPOTENT FIX: Check if table exists before modifying it
            // If the table doesn't exist, create it first
            migrationBuilder.Sql(@"
                -- Create user_tenant_roles table if it doesn't exist
                DO $$
                BEGIN
                    IF NOT EXISTS (
                        SELECT FROM information_schema.tables
                        WHERE table_schema = 'identity'
                        AND table_name = 'user_tenant_roles'
                    ) THEN
                        -- Create the table
                        CREATE TABLE identity.user_tenant_roles (
                            id uuid NOT NULL PRIMARY KEY,
                            user_id uuid NOT NULL,
                            tenant_id uuid NOT NULL,
                            role character varying(50) NOT NULL,
                            assigned_at timestamp with time zone NOT NULL,
                            assigned_by_user_id uuid,
                            CONSTRAINT uq_user_tenant_roles_user_tenant UNIQUE (user_id, tenant_id)
                        );

                        -- Create basic indexes
                        -- Note: ix_user_tenant_roles_tenant_role will be created by a later migration
                        CREATE INDEX ix_user_tenant_roles_user_id ON identity.user_tenant_roles(user_id);
                        CREATE INDEX ix_user_tenant_roles_tenant_id ON identity.user_tenant_roles(tenant_id);
                        CREATE INDEX ix_user_tenant_roles_role ON identity.user_tenant_roles(role);
                    END IF;
                END $$;
            ");

            // Drop existing foreign keys if they exist
            migrationBuilder.Sql(@"
                DO $$
                BEGIN
                    -- Drop FK to tenants if it exists
                    IF EXISTS (
                        SELECT FROM information_schema.table_constraints
                        WHERE constraint_schema = 'identity'
                        AND table_name = 'user_tenant_roles'
                        AND constraint_name = 'FK_user_tenant_roles_tenants_tenant_id'
                    ) THEN
                        ALTER TABLE identity.user_tenant_roles
                        DROP CONSTRAINT ""FK_user_tenant_roles_tenants_tenant_id"";
                    END IF;

                    -- Drop FK to users if it exists
                    IF EXISTS (
                        SELECT FROM information_schema.table_constraints
                        WHERE constraint_schema = 'identity'
                        AND table_name = 'user_tenant_roles'
                        AND constraint_name = 'FK_user_tenant_roles_users_user_id'
                    ) THEN
                        ALTER TABLE identity.user_tenant_roles
                        DROP CONSTRAINT ""FK_user_tenant_roles_users_user_id"";
                    END IF;
                END $$;
            ");

            // Recreate foreign keys with correct column references
            // Note: At this point in time, users and tenants are still in the default schema
            // (They will be moved to identity schema in a later migration)
            migrationBuilder.AddForeignKey(
                name: "FK_user_tenant_roles_users_user_id",
                schema: "identity",
                table: "user_tenant_roles",
                column: "user_id",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_user_tenant_roles_tenants_tenant_id",
                schema: "identity",
                table: "user_tenant_roles",
                column: "tenant_id",
                principalTable: "tenants",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Drop foreign keys if they exist
            migrationBuilder.Sql(@"
                DO $$
                BEGIN
                    IF EXISTS (
                        SELECT FROM information_schema.table_constraints
                        WHERE constraint_schema = 'identity'
                        AND table_name = 'user_tenant_roles'
                        AND constraint_name = 'FK_user_tenant_roles_tenants_tenant_id'
                    ) THEN
                        ALTER TABLE identity.user_tenant_roles
                        DROP CONSTRAINT ""FK_user_tenant_roles_tenants_tenant_id"";
                    END IF;

                    IF EXISTS (
                        SELECT FROM information_schema.table_constraints
                        WHERE constraint_schema = 'identity'
                        AND table_name = 'user_tenant_roles'
                        AND constraint_name = 'FK_user_tenant_roles_users_user_id'
                    ) THEN
                        ALTER TABLE identity.user_tenant_roles
                        DROP CONSTRAINT ""FK_user_tenant_roles_users_user_id"";
                    END IF;
                END $$;
            ");

            // Note: We don't drop the table in Down() because it should have been created
            // by a previous migration. If it was created by this migration (first run),
            // then it will be cleaned up when the database is reset.
        }
    }
}
