using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MemberCenter.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddAuthResourceRegistry : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "auth_client_usage_permissions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Usage = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Scope = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_auth_client_usage_permissions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "auth_resources",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Audience = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    RequireTenant = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    AllowDelegatedToken = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_auth_resources", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "auth_resource_scopes",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ResourceId = table.Column<Guid>(type: "uuid", nullable: false),
                    Scope = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_auth_resource_scopes", x => x.Id);
                    table.ForeignKey(
                        name: "FK_auth_resource_scopes_auth_resources_ResourceId",
                        column: x => x.ResourceId,
                        principalTable: "auth_resources",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_auth_client_usage_permissions_Usage_Scope",
                table: "auth_client_usage_permissions",
                columns: new[] { "Usage", "Scope" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_auth_resource_scopes_scope",
                table: "auth_resource_scopes",
                column: "Scope");

            migrationBuilder.CreateIndex(
                name: "IX_auth_resource_scopes_ResourceId_Scope",
                table: "auth_resource_scopes",
                columns: new[] { "ResourceId", "Scope" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_auth_resources_Audience",
                table: "auth_resources",
                column: "Audience",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_auth_resources_Name",
                table: "auth_resources",
                column: "Name",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "auth_resource_scopes");

            migrationBuilder.DropTable(
                name: "auth_resources");
        }
    }
}
