<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Members Table
        Schema::create('members', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('member_id')->unique(); // membership ID e.g., LPTA-00001
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->string('gender')->nullable();
            $table->date('dob')->nullable();
            $table->string('address')->nullable();
            $table->string('belt_rank')->default('White'); // White, Yellow, Green, Blue, Red, Black
            $table->foreignId('district_id')->constrained()->onDelete('cascade');
            $table->foreignId('club_id')->nullable()->constrained()->onDelete('set null');
            $table->string('photo')->nullable();
            $table->string('qr_code_path')->nullable();
            $table->string('status')->default('pending'); // pending, active, expired
            $table->date('expiry_date')->nullable();
            $table->timestamps();
        });

        // 2. Referees Table
        Schema::create('referees', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('certification_level'); // e.g. National Class A, Class B, International
            $table->integer('experience_years')->default(0);
            $table->string('contact_phone')->nullable();
            $table->string('email')->nullable();
            $table->string('photo')->nullable();
            $table->text('bio')->nullable();
            $table->string('status')->default('active'); // active, inactive
            $table->timestamps();
        });

        // 3. Coaches Table
        Schema::create('coaches', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('qualification');
            $table->integer('experience_years')->default(0);
            $table->text('achievements')->nullable();
            $table->string('specialization')->nullable(); // Poomsae, Kyorugi, etc.
            $table->string('contact_phone')->nullable();
            $table->string('email')->nullable();
            $table->string('photo')->nullable();
            $table->text('bio')->nullable();
            $table->foreignId('club_id')->nullable()->constrained()->onDelete('set null');
            $table->string('status')->default('active'); // active, inactive
            $table->timestamps();
        });

        // 4. News Categories Table
        Schema::create('news_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->timestamps();
        });

        // 5. News Table
        Schema::create('news', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->constrained('news_categories')->onDelete('cascade');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('content');
            $table->string('image')->nullable();
            $table->boolean('is_featured')->default(false);
            $table->integer('views')->default(0);
            $table->foreignId('author_id')->constrained('users')->onDelete('cascade');
            $table->string('status')->default('published'); // published, draft
            $table->timestamps();
        });

        // 6. Albums Table
        Schema::create('albums', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->string('cover_image')->nullable();
            $table->timestamps();
        });

        // 7. Gallery Items Table
        Schema::create('gallery_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('album_id')->constrained()->onDelete('cascade');
            $table->string('type')->default('image'); // image, video
            $table->string('file_path');
            $table->string('video_url')->nullable();
            $table->string('caption')->nullable();
            $table->timestamps();
        });

        // 8. Events Table
        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('description');
            $table->dateTime('start_date');
            $table->dateTime('end_date');
            $table->string('venue');
            $table->string('registration_status')->default('closed'); // open, closed
            $table->string('image')->nullable();
            $table->timestamps();
        });

        // 9. Achievements Table
        Schema::create('achievements', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('type')->default('national'); // national, international
            $table->string('medal_type')->default('none'); // gold, silver, bronze, none
            $table->string('player_name');
            $table->date('achievement_date');
            $table->string('photo')->nullable();
            $table->timestamps();
        });

        // 10. Documents Table (Downloads)
        Schema::create('documents', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('file_path');
            $table->string('file_type')->default('pdf'); // pdf, docx, etc.
            $table->text('description')->nullable();
            $table->timestamps();
        });

        // 11. Activity Logs Table
        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('action');
            $table->text('details')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('activity_logs');
        Schema::dropIfExists('documents');
        Schema::dropIfExists('achievements');
        Schema::dropIfExists('events');
        Schema::dropIfExists('gallery_items');
        Schema::dropIfExists('albums');
        Schema::dropIfExists('news');
        Schema::dropIfExists('news_categories');
        Schema::dropIfExists('coaches');
        Schema::dropIfExists('referees');
        Schema::dropIfExists('members');
    }
};
