<?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('client_groups', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->text('description')->nullable();
            $table->decimal('minimum_savings', 15, 2)->default(0);
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->string('phone')->nullable();
            $table->boolean('sms_notifications')->default(true);
            $table->boolean('email_notifications')->default(true);
            $table->foreignId('created_by')->constrained('users')->onDelete('cascade');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('client_group_members', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_group_id')->constrained()->onDelete('cascade');
            $table->foreignId('client_id')->constrained()->onDelete('cascade');
            $table->enum('role', ['leader', 'secretary', 'treasurer', 'member'])->default('member');
            $table->date('joined_date');
            $table->date('left_date')->nullable();
            $table->timestamps();
            $table->unique(['client_group_id', 'client_id']);
        });
    }

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