using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.Sql("""
                INSERT INTO content_entries (
                    id, tenant_id, entry_key, name, entry_type, visibility,
                    access_rules, layout_config, sort_order, is_active, created_at, updated_at)
                SELECT
                    qb.id,
                    qb.tenant_id,
                    'public-question-bank-' || replace(qb.id::text, '-', ''),
                    qb.name,
                    'question_practice',
                    'public',
                    '{}'::jsonb,
                    '{}'::jsonb,
                    0,
                    true,
                    qb.created_at,
                    qb.updated_at
                FROM question_banks qb
                JOIN tenants t ON t.id = qb.tenant_id AND t.mode = 'platform_owned'
                WHERE NOT EXISTS (SELECT 1 FROM content_entries ce WHERE ce.id = qb.id);

                UPDATE question_banks qb
                SET content_entry_id = qb.id
                FROM tenants t
                WHERE t.id = qb.tenant_id
                  AND t.mode = 'platform_owned'
                  AND qb.content_entry_id IS NULL
                  AND EXISTS (SELECT 1 FROM content_entries ce WHERE ce.tenant_id = qb.tenant_id AND ce.id = qb.id);
                """);

            migrationBuilder.CreateIndex(
                name: "ix_question_banks_tenant_id_content_entry_id",
                table: "question_banks",
                columns: new[] { "tenant_id", "content_entry_id" });

            migrationBuilder.AddForeignKey(
                name: "fk_question_banks_content_entries_tenant_id_content_entry_id",
                table: "question_banks",
                columns: new[] { "tenant_id", "content_entry_id" },
                principalTable: "content_entries",
                principalColumns: new[] { "tenant_id", "id" },
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropIndex(
                name: "ix_question_banks_tenant_id_content_entry_id",
                table: "question_banks");

            migrationBuilder.DropColumn(
                name: "content_entry_id",
                table: "question_banks");
        }
    }
}
