using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class CommercialDeliveryClosure : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "idempotency_key",
                table: "platform_billing_refunds",
                type: "character varying(200)",
                maxLength: 200,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "last_error",
                table: "platform_billing_refunds",
                type: "character varying(2000)",
                maxLength: 2000,
                nullable: true);

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

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

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

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

            migrationBuilder.AddColumn<string>(
                name: "subscription_effect",
                table: "platform_billing_refunds",
                type: "character varying(32)",
                maxLength: 32,
                nullable: true);

            migrationBuilder.Sql(
                """
                UPDATE platform_billing_refunds
                SET idempotency_key = 'legacy:' || id::text,
                    subscription_effect = 'keep_service'
                WHERE idempotency_key IS NULL OR subscription_effect IS NULL;
                """);

            migrationBuilder.AlterColumn<string>(
                name: "idempotency_key",
                table: "platform_billing_refunds",
                type: "character varying(200)",
                maxLength: 200,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(200)",
                oldMaxLength: 200,
                oldNullable: true);

            migrationBuilder.AlterColumn<string>(
                name: "subscription_effect",
                table: "platform_billing_refunds",
                type: "character varying(32)",
                maxLength: 32,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(32)",
                oldMaxLength: 32,
                oldNullable: true);

            migrationBuilder.CreateTable(
                name: "platform_operation_idempotencies",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    actor_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    scope = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    idempotency_key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    request_hash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    resource_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_platform_operation_idempotencies", x => x.id);
                    table.ForeignKey(
                        name: "fk_platform_operation_idempotencies_users_actor_user_id",
                        column: x => x.actor_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "tenant_billing_policies",
                columns: table => new
                {
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    collection_mode = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    default_payment_provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    auto_generate_renewal = table.Column<bool>(type: "boolean", nullable: false),
                    renewal_lead_days = table.Column<int>(type: "integer", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_tenant_billing_policies", x => x.tenant_id);
                    table.CheckConstraint("ck_tenant_billing_policies_renewal_lead_days", "renewal_lead_days between 1 and 90");
                    table.ForeignKey(
                        name: "fk_tenant_billing_policies_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "tenant_owner_activation_grants",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_by = table.Column<Guid>(type: "uuid", nullable: false),
                    token_hash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    expires_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    consumed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_tenant_owner_activation_grants", x => x.id);
                    table.UniqueConstraint("ak_tenant_owner_activation_grants_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.ForeignKey(
                        name: "fk_tenant_owner_activation_grants_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_tenant_owner_activation_grants_users_created_by",
                        column: x => x.created_by,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "fk_tenant_owner_activation_grants_users_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_platform_billing_refunds_tenant_id_idempotency_key",
                table: "platform_billing_refunds",
                columns: new[] { "tenant_id", "idempotency_key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_operation_idempotencies_actor_user_id_scope_idempo~",
                table: "platform_operation_idempotencies",
                columns: new[] { "actor_user_id", "scope", "idempotency_key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_created_by",
                table: "tenant_owner_activation_grants",
                column: "created_by");

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_tenant_id_user_id_consumed_a~",
                table: "tenant_owner_activation_grants",
                columns: new[] { "tenant_id", "user_id", "consumed_at", "expires_at" });

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_token_hash",
                table: "tenant_owner_activation_grants",
                column: "token_hash",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_tenant_owner_activation_grants_user_id",
                table: "tenant_owner_activation_grants",
                column: "user_id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "platform_operation_idempotencies");

            migrationBuilder.DropTable(
                name: "tenant_billing_policies");

            migrationBuilder.DropTable(
                name: "tenant_owner_activation_grants");

            migrationBuilder.DropIndex(
                name: "ix_platform_billing_refunds_tenant_id_idempotency_key",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "idempotency_key",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "last_error",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "requested_by",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "review_reason",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "reviewed_at",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "reviewed_by",
                table: "platform_billing_refunds");

            migrationBuilder.DropColumn(
                name: "subscription_effect",
                table: "platform_billing_refunds");
        }
    }
}
