<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
 * Two columns on `projects` for the kit-authoring sync feature:
 *
 *   - `is_blank` — true when the project was created without applying a
 *     kit (blank project). Set once at create time, immutable thereafter.
 *     Drives visibility of the sync_new_layers toggle in Project Settings
 *     so template users (kit-applied projects) never see the affordance.
 *
 *   - `sync_new_layers` — when on, every new layer added on a non-marketing
 *     factor auto-propagates to other non-marketing factors via axis-scaled
 *     anchors. Frame layers also get family-resolved devices on the target
 *     factors. Default off — advanced feature for kit authors who want to
 *     compose a multi-factor kit without re-authoring each factor.
 *
 * Both columns are nullable on existing rows (backfilled below) — pre-pivot
 * projects pre-date the blank-vs-kit distinction so we cannot reliably
 * classify them; treat them as not-blank (sync hidden) which keeps the
 * existing UX intact.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->boolean('is_blank')->default(false)->after('active_form_factors');
            $table->boolean('sync_new_layers')->default(false)->after('is_blank');
        });

        // Existing projects keep their is_blank=false default. No way to
        // tell retroactively whether a row was kit-created or blank, and
        // hiding the sync toggle on legacy rows is the safe path.
        DB::statement('UPDATE projects SET is_blank = 0, sync_new_layers = 0 WHERE is_blank IS NULL');
    }

    public function down(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropColumn(['is_blank', 'sync_new_layers']);
        });
    }
};
