<?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('sports_matches', function (Blueprint $table) {
            $table->id();

            // A match always belongs to a video (type=match) and its creator.
            $table->foreignId('video_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();

            // draft | published — lets uploaders save basic info and finish later.
            $table->string('status', 20)->default('draft')->index();

            // ── Basic fields (shown first; only title is required for a draft) ───
            $table->string('title');                         // match title (required)
            $table->string('event_name')->nullable();
            $table->date('match_date')->nullable();
            $table->time('match_time')->nullable();
            $table->string('participant1_name')->nullable();
            $table->string('participant2_name')->nullable();
            $table->string('referee_name')->nullable();
            // ── Fields revealed later when editing ──────────────────────────────
            $table->string('sport', 80)->nullable()->index();
            $table->string('match_type', 80)->nullable();
            $table->string('venue_name')->nullable();

            // ── Optional groups (progressive disclosure) stored as JSON ──────────
            $table->json('competition')->nullable();   // name, type, stage, season, organizer, championship_name
            $table->json('participants')->nullable();  // p1_*/p2_* details, weight_class, gender_division, level, extra[]
            $table->json('media')->nullable();          // image paths + caption, alt, credit, public
            $table->json('officials')->nullable();      // [{ role, name, photo }]
            $table->json('venue')->nullable();          // address, city, country, lat, lng, notes
            $table->json('result')->nullable();         // winner, outcome_type, final_result, rank, notes
            $table->json('segments')->nullable();       // [{ type, number, score, winner, notes }]
            $table->json('statistics')->nullable();     // [{ name, value, owner, notes }]
            $table->json('reviews')->nullable();        // review_type, requested_by, review_result, source_url, verification_notes, admin_notes

            $table->timestamps();
        });
    }

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