<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('tiers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique(); // invited, indulged, limitless
            $table->decimal('min_spend_per_visit', 8, 2);
            $table->integer('visit_frequency_months'); // must visit once every X months
            $table->integer('visits_per_period')->default(1); // required visits per period
            $table->integer('points_threshold_3months'); // points needed to maintain tier
            $table->integer('display_order');
            $table->string('color_class')->nullable(); // for UI badge coloring
            $table->timestamps();
        });

        Schema::create('member_tiers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('tier_id')->constrained();
            $table->boolean('is_current')->default(true);
            $table->foreignId('assigned_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->enum('assignment_type', ['auto', 'manual', 'initial'])->default('initial');
            $table->text('reason')->nullable();
            $table->timestamp('assigned_at');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('member_tiers');
        Schema::dropIfExists('tiers');
    }
};
