using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class EnforceProductModuleCatalog : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("""
                INSERT INTO product_modules
                    (id, code, name, status, sort_order, created_at, updated_at)
                VALUES
                    ('10000000-0000-0000-0000-000000000001', 'dashboard', 'Dashboard', 'active', 10, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000002', 'staff', 'Staff', 'active', 20, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000003', 'role', 'Roles', 'active', 30, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000004', 'student', 'Students', 'active', 40, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000005', 'content', 'Content', 'active', 50, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000006', 'settings', 'Settings', 'active', 60, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000007', 'provider', 'Providers', 'active', 70, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000008', 'commerce', 'Commerce', 'active', 80, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-000000000009', 'crm', 'CRM', 'active', 90, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-00000000000a', 'commission', 'Commission', 'active', 100, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
                    ('10000000-0000-0000-0000-00000000000b', 'job', 'Background Jobs', 'active', 110, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
                ON CONFLICT (code) DO UPDATE SET
                    name = EXCLUDED.name,
                    status = 'active',
                    sort_order = EXCLUDED.sort_order,
                    updated_at = CURRENT_TIMESTAMP;

                INSERT INTO plan_module_entitlements (id, plan_code, module_code, enabled)
                SELECT
                    md5('plan-module:' || plan.code || ':' || module.code)::uuid,
                    plan.code,
                    module.code,
                    TRUE
                FROM platform_saas_plans AS plan
                CROSS JOIN product_modules AS module
                WHERE module.code IN
                    ('dashboard', 'staff', 'role', 'student', 'content', 'settings',
                     'provider', 'commerce', 'crm', 'commission', 'job')
                  AND NOT EXISTS (
                      SELECT 1
                      FROM plan_module_entitlements AS existing
                      WHERE existing.plan_code = plan.code)
                ON CONFLICT (plan_code, module_code) DO NOTHING;
                """);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("""
                DELETE FROM product_modules
                WHERE code IN
                    ('dashboard', 'staff', 'role', 'student', 'content', 'settings',
                     'provider', 'commerce', 'crm', 'commission', 'job');
                """);
        }
    }
}
