<?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('clubs', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->text('description')->nullable();
            $table->string('category')->nullable()->index();
            $table->foreignId('patron_id')->nullable()->constrained('staff')->nullOnDelete();
            $table->foreignId('academic_year_id')->nullable()->constrained()->nullOnDelete();
            $table->unsignedSmallInteger('max_members')->nullable();
            $table->string('status')->default('active')->index();
            $table->boolean('discussion_enabled')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('club_student', function (Blueprint $table) {
            $table->id();
            $table->foreignId('club_id')->constrained()->cascadeOnDelete();
            $table->foreignId('student_id')->constrained()->cascadeOnDelete();
            $table->string('role')->default('member')->index();
            $table->date('joined_on')->nullable();
            $table->string('status')->default('active')->index();
            $table->timestamps();

            $table->unique(['club_id', 'student_id']);
        });

        Schema::create('club_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('club_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->text('body')->nullable();
            $table->string('message_type')->default('text')->index();
            $table->foreignId('parent_id')->nullable()->constrained('club_messages')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['club_id', 'created_at']);
        });

        Schema::create('club_message_attachments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('club_message_id')->constrained()->cascadeOnDelete();
            $table->string('file_path');
            $table->string('file_name');
            $table->string('mime_type')->nullable();
            $table->unsignedBigInteger('file_size')->default(0);
            $table->string('attachment_type')->default('document')->index();
            $table->timestamps();
        });

        Schema::create('club_message_reactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('club_message_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('emoji', 16);
            $table->timestamps();

            $table->unique(['club_message_id', 'user_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('club_message_reactions');
        Schema::dropIfExists('club_message_attachments');
        Schema::dropIfExists('club_messages');
        Schema::dropIfExists('club_student');
        Schema::dropIfExists('clubs');
    }
};
