<?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('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('permissions', function (Blueprint $table) {
            $table->id();
            $table->string('module');
            $table->string('action');
            $table->string('label');
            $table->timestamps();
            $table->unique(['module', 'action']);
        });

        Schema::create('permission_role', function (Blueprint $table) {
            $table->id();
            $table->foreignId('permission_id')->constrained()->cascadeOnDelete();
            $table->foreignId('role_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['permission_id', 'role_id']);
        });

        Schema::create('user_positions', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::table('users', function (Blueprint $table) {
            $table->string('first_name')->nullable()->after('id');
            $table->string('middle_name')->nullable()->after('first_name');
            $table->string('last_name')->nullable()->after('middle_name');
            $table->string('phone')->nullable()->after('email');
            $table->string('username')->nullable()->unique()->after('phone');
            $table->foreignId('role_id')->nullable()->after('username')->constrained()->nullOnDelete();
            $table->foreignId('user_position_id')->nullable()->after('role_id')->constrained()->nullOnDelete();
            $table->string('status')->default('active')->after('user_position_id');
            $table->timestamp('last_login_at')->nullable()->after('status');
        });

        Schema::create('member_positions', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('service_types', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('sacrament_types', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->text('description')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('jumuiyas', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->string('leader_name')->nullable();
            $table->string('assistant_leader')->nullable();
            $table->string('phone')->nullable();
            $table->string('location')->nullable();
            $table->string('status')->default('active');
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('members', function (Blueprint $table) {
            $table->id();
            $table->string('member_code')->unique();
            $table->string('first_name');
            $table->string('middle_name')->nullable();
            $table->string('last_name');
            $table->string('gender')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->text('address')->nullable();
            $table->foreignId('jumuiya_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('member_position_id')->nullable()->constrained()->nullOnDelete();
            $table->boolean('is_baptized')->default(false);
            $table->boolean('is_confirmed')->default(false);
            $table->boolean('is_allowed_sacrament')->default(false);
            $table->string('qr_token')->unique();
            $table->string('status')->default('active');
            $table->date('date_registered')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('church_services', function (Blueprint $table) {
            $table->id();
            $table->foreignId('service_type_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name');
            $table->date('service_date');
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            $table->string('location')->default('Anglican Church St Peter and Paul Mwenge Kijitonyama');
            $table->text('description')->nullable();
            $table->string('status')->default('open');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('attendance_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->constrained()->cascadeOnDelete();
            $table->foreignId('church_service_id')->constrained()->cascadeOnDelete();
            $table->date('attendance_date');
            $table->time('time_in')->nullable();
            $table->string('method')->default('manual');
            $table->foreignId('marked_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('status')->default('present');
            $table->text('remarks')->nullable();
            $table->timestamps();
            $table->unique(['member_id', 'church_service_id']);
        });

        Schema::create('sacrament_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->constrained()->cascadeOnDelete();
            $table->foreignId('church_service_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('sacrament_type_id')->constrained()->cascadeOnDelete();
            $table->date('sacrament_date');
            $table->string('status')->default('participated');
            $table->foreignId('recorded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('remarks')->nullable();
            $table->timestamps();
        });

        Schema::create('church_profiles', function (Blueprint $table) {
            $table->id();
            $table->string('church_name')->default('Anglican Church St Peter and Paul');
            $table->string('diocese')->nullable();
            $table->string('parish')->nullable();
            $table->string('location')->default('Mwenge Kijitonyama');
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->text('address')->nullable();
            $table->string('logo_path')->nullable();
            $table->timestamps();
        });

        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('group');
            $table->string('key');
            $table->text('value')->nullable();
            $table->timestamps();
            $table->unique(['group', 'key']);
        });

        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('action');
            $table->string('module');
            $table->text('description')->nullable();
            $table->string('ip_address')->nullable();
            $table->text('user_agent')->nullable();
            $table->json('properties')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('activity_logs');
        Schema::dropIfExists('settings');
        Schema::dropIfExists('church_profiles');
        Schema::dropIfExists('sacrament_records');
        Schema::dropIfExists('attendance_records');
        Schema::dropIfExists('church_services');
        Schema::dropIfExists('members');
        Schema::dropIfExists('jumuiyas');
        Schema::dropIfExists('sacrament_types');
        Schema::dropIfExists('service_types');
        Schema::dropIfExists('member_positions');

        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['role_id']);
            $table->dropForeign(['user_position_id']);
            $table->dropColumn([
                'first_name',
                'middle_name',
                'last_name',
                'phone',
                'username',
                'role_id',
                'user_position_id',
                'status',
                'last_login_at',
            ]);
        });

        Schema::dropIfExists('permission_role');
        Schema::dropIfExists('permissions');
        Schema::dropIfExists('roles');
        Schema::dropIfExists('user_positions');
    }
};
