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)
        {
            // Drop and recreate foreign keys to ensure they reference the correct columns
            // This fixes BUG-002: Foreign keys were incorrectly referencing user_id1/tenant_id1

            migrationBuilder.DropForeignKey(
                name: "FK_user_tenant_roles_tenants_tenant_id",
                schema: "identity",
                table: "user_tenant_roles");

            migrationBuilder.DropForeignKey(
                name: "FK_user_tenant_roles_users_user_id",
                schema: "identity",
                table: "user_tenant_roles");

            // Recreate foreign keys with correct column references
            // Note: users and tenants tables are in the default schema (no explicit schema)
            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)
        {
            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);

            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);
        }
    }
}
