using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.IdentityDb
{
    /// <inheritdoc />
    public partial class GrantAnnouncementPermissionsToSuperAdmin : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                @"WITH target_roles AS (
    SELECT ""Id"" AS role_id, ""TenantId"" AS tenant_id
    FROM ""roles""
    WHERE ""Code"" IN ('super-admin', 'SUPER_ADMIN', 'PlatformAdmin', 'platform-admin')
      AND ""DeletedAt"" IS NULL
),
 target_permissions AS (
    SELECT DISTINCT tr.tenant_id, pc.code
    FROM target_roles tr
    CROSS JOIN (VALUES
        ('platform-announcement:create'),
        ('platform-announcement:publish'),
        ('platform-announcement:revoke'),
        ('tenant-announcement:publish'),
        ('tenant-announcement:revoke')
    ) AS pc(code)
)
INSERT INTO ""permissions"" (
    ""TenantId"",
    ""Name"",
    ""Code"",
    ""Description"",
    ""CreatedAt"",
    ""CreatedBy"",
    ""UpdatedAt"",
    ""UpdatedBy"",
    ""DeletedAt"",
    ""DeletedBy""
)
SELECT
    tp.tenant_id,
    tp.code,
    tp.code,
    CONCAT('Seed permission ', tp.code),
    NOW(),
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
FROM target_permissions tp
ON CONFLICT (""TenantId"", ""Code"") DO NOTHING;"
            );
            migrationBuilder.Sql(
                @"WITH target_roles AS (
    SELECT ""Id"" AS role_id, ""TenantId"" AS tenant_id
    FROM ""roles""
    WHERE ""Code"" IN ('super-admin', 'SUPER_ADMIN', 'PlatformAdmin', 'platform-admin')
      AND ""DeletedAt"" IS NULL
)
INSERT INTO ""role_permissions"" (
    ""TenantId"",
    ""RoleId"",
    ""PermissionId"",
    ""CreatedAt"",
    ""CreatedBy"",
    ""UpdatedAt"",
    ""UpdatedBy"",
    ""DeletedAt"",
    ""DeletedBy""
)
SELECT
    tr.tenant_id,
    tr.role_id,
    p.""Id"",
    NOW(),
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
FROM target_roles tr
JOIN ""permissions"" p
  ON p.""TenantId"" = tr.tenant_id
 AND p.""Code"" IN (
    'platform-announcement:create',
    'platform-announcement:publish',
    'platform-announcement:revoke',
    'tenant-announcement:publish',
    'tenant-announcement:revoke'
 )
WHERE p.""DeletedAt"" IS NULL
ON CONFLICT (""TenantId"", ""RoleId"", ""PermissionId"") DO NOTHING;"
            );
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                @"WITH target_roles AS (
    SELECT ""Id"" AS role_id, ""TenantId"" AS tenant_id
    FROM ""roles""
    WHERE ""Code"" IN ('super-admin', 'SUPER_ADMIN', 'PlatformAdmin', 'platform-admin')
      AND ""DeletedAt"" IS NULL
),
 target_permissions AS (
    SELECT ""Id"" AS permission_id, ""TenantId"" AS tenant_id
    FROM ""permissions""
    WHERE ""Code"" IN (
        'platform-announcement:create',
        'platform-announcement:publish',
        'platform-announcement:revoke',
        'tenant-announcement:publish',
        'tenant-announcement:revoke'
    )
)
DELETE FROM ""role_permissions"" rp
USING target_roles tr, target_permissions tp
WHERE rp.""TenantId"" = tr.tenant_id
  AND rp.""RoleId"" = tr.role_id
  AND rp.""PermissionId"" = tp.permission_id;"
            );
        }
    }
}
