using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class TenantDomainDrivenOwnerActivation : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<Guid>(
                name: "domain_id",
                table: "tenant_owner_activation_grants",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "revocation_reason",
                table: "tenant_owner_activation_grants",
                type: "character varying(1000)",
                maxLength: 1000,
                nullable: true);

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "revoked_at",
                table: "tenant_owner_activation_grants",
                type: "timestamp with time zone",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "revoked_by",
                table: "tenant_owner_activation_grants",
                type: "uuid",
                nullable: true);

            migrationBuilder.Sql("""
                update tenant_owner_activation_grants
                set revoked_at = now(),
                    revocation_reason = 'Revoked by tenant domain driven activation migration',
                    updated_at = now()
                where consumed_at is null;
                """);

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_revoked_by",
                table: "tenant_owner_activation_grants",
                column: "revoked_by");

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_tenant_id_domain_id",
                table: "tenant_owner_activation_grants",
                columns: new[] { "tenant_id", "domain_id" });

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_tenant_id_user_id",
                table: "tenant_owner_activation_grants",
                columns: new[] { "tenant_id", "user_id" },
                unique: true,
                filter: "consumed_at is null and revoked_at is null");

            migrationBuilder.AddForeignKey(
                name: "fk_tenant_owner_activation_grants_tenant_domains_tenant_id_dom~",
                table: "tenant_owner_activation_grants",
                columns: new[] { "tenant_id", "domain_id" },
                principalTable: "tenant_domains",
                principalColumns: new[] { "tenant_id", "id" },
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "fk_tenant_owner_activation_grants_users_revoked_by",
                table: "tenant_owner_activation_grants",
                column: "revoked_by",
                principalTable: "users",
                principalColumn: "id",
                onDelete: ReferentialAction.Restrict);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "fk_tenant_owner_activation_grants_tenant_domains_tenant_id_dom~",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropForeignKey(
                name: "fk_tenant_owner_activation_grants_users_revoked_by",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropIndex(
                name: "ix_tenant_owner_activation_grants_revoked_by",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropIndex(
                name: "ix_tenant_owner_activation_grants_tenant_id_domain_id",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropIndex(
                name: "ix_tenant_owner_activation_grants_tenant_id_user_id",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropColumn(
                name: "domain_id",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropColumn(
                name: "revocation_reason",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropColumn(
                name: "revoked_at",
                table: "tenant_owner_activation_grants");

            migrationBuilder.DropColumn(
                name: "revoked_by",
                table: "tenant_owner_activation_grants");
        }
    }
}
