<?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('audit_logs', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->nullable()->constrained()->nullOnDelete(); $t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $t->string('action',40); $t->string('module',80); $t->text('description'); $t->string('subject_type')->nullable(); $t->unsignedBigInteger('subject_id')->nullable();
            $t->string('ip_address',45)->nullable(); $t->text('user_agent')->nullable(); $t->json('meta')->nullable(); $t->timestamp('created_at')->useCurrent(); $t->index(['organization_id','created_at']);
        });
        Schema::create('whatsapp_logs', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->constrained()->cascadeOnDelete(); $t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('member_id')->constrained()->cascadeOnDelete(); $t->text('message')->nullable(); $t->timestamp('clicked_at')->useCurrent(); $t->index(['organization_id','clicked_at']);
        });
        Schema::create('attendance_sessions', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->constrained()->cascadeOnDelete(); $t->foreignId('activity_id')->nullable()->constrained()->nullOnDelete();
            $t->string('title'); $t->string('token',32)->unique(); $t->dateTime('opens_at')->nullable(); $t->dateTime('closes_at')->nullable(); $t->string('location_name')->nullable();
            $t->decimal('latitude',10,7)->nullable(); $t->decimal('longitude',10,7)->nullable(); $t->unsignedInteger('radius_meters')->default(300);
            $t->boolean('require_location')->default(true); $t->boolean('require_photo')->default(true); $t->boolean('one_device_one_person')->default(true);
            $t->boolean('allow_public')->default(true); $t->boolean('is_active')->default(true); $t->unsignedTinyInteger('retention_months')->default(6); $t->timestamps();
        });
        Schema::create('attendance_entries', function (Blueprint $t) {
            $t->id(); $t->foreignId('attendance_session_id')->constrained()->cascadeOnDelete(); $t->foreignId('member_id')->nullable()->constrained()->nullOnDelete();
            $t->string('full_name'); $t->string('nickname')->nullable(); $t->string('organization_name')->nullable(); $t->string('position')->nullable(); $t->string('whatsapp',40)->nullable();
            $t->string('device_hash',64); $t->decimal('latitude',10,7)->nullable(); $t->decimal('longitude',10,7)->nullable(); $t->unsignedInteger('distance_meters')->nullable();
            $t->string('photo_path')->nullable(); $t->string('status',30)->default('present'); $t->timestamp('attended_at')->useCurrent(); $t->string('ip_address',45)->nullable(); $t->text('user_agent')->nullable();
            $t->timestamps(); $t->unique(['attendance_session_id','device_hash'],'attendance_device_unique');
        });
    }
    public function down(): void { Schema::dropIfExists('attendance_entries'); Schema::dropIfExists('attendance_sessions'); Schema::dropIfExists('whatsapp_logs'); Schema::dropIfExists('audit_logs'); }
};
