<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('audit_logs', function (Blueprint $table) {
            $table->id();
            $table->string('category'); // admin_action, verification, payment, dispute, user_activity
            $table->string('user');
            $table->text('action');
            $table->string('ip_address')->nullable();
            $table->timestamps();
        });

        // Dynamic seeding from active database tables
        // 1. Workers registration logs
        $workers = DB::table('workers')->get();
        foreach ($workers as $worker) {
            DB::table('audit_logs')->insert([
                'category' => 'user_activity',
                'user' => $worker->name . ' (Worker)',
                'action' => 'Worker registered successfully. Nationality: ' . $worker->nationality . ', Category: ' . $worker->category_id,
                'ip_address' => '94.200.12.' . rand(10, 250),
                'created_at' => $worker->created_at ?: now(),
                'updated_at' => $worker->created_at ?: now(),
            ]);

            if ($worker->verified) {
                DB::table('audit_logs')->insert([
                    'category' => 'verification',
                    'user' => 'System Verification Engine',
                    'action' => 'Verification documents and credentials verification approved for worker: ' . $worker->name,
                    'ip_address' => 'Auto-Trigger',
                    'created_at' => $worker->created_at ?: now(),
                    'updated_at' => $worker->created_at ?: now(),
                ]);
            }
        }

        // 2. Sponsors registration & plan logs
        $sponsors = DB::table('sponsors')->get();
        foreach ($sponsors as $sponsor) {
            DB::table('audit_logs')->insert([
                'category' => 'user_activity',
                'user' => $sponsor->full_name . ' (Sponsor)',
                'action' => 'Sponsor account created successfully. Mobile: ' . $sponsor->mobile,
                'ip_address' => '91.74.203.' . rand(10, 250),
                'created_at' => $sponsor->created_at ?: now(),
                'updated_at' => $sponsor->created_at ?: now(),
            ]);

            if ($sponsor->subscription_status === 'active') {
                DB::table('audit_logs')->insert([
                    'category' => 'payment',
                    'user' => 'Stripe / Dubai Gateway',
                    'action' => 'Plan ' . strtoupper($sponsor->subscription_plan) . ' active subscription verified. Status: Paid',
                    'ip_address' => 'Stripe Gateway',
                    'created_at' => $sponsor->subscription_start_date ?: now(),
                    'updated_at' => $sponsor->subscription_start_date ?: now(),
                ]);
            }
        }

        // 3. Payments logs
        $payments = DB::table('payments')->get();
        foreach ($payments as $payment) {
            DB::table('audit_logs')->insert([
                'category' => 'payment',
                'user' => 'Stripe Webhook',
                'action' => 'Subscription payment charge of ' . $payment->amount . ' ' . $payment->currency . ' status: ' . strtoupper($payment->status),
                'ip_address' => 'Stripe Webhook API',
                'created_at' => $payment->created_at ?: now(),
                'updated_at' => $payment->created_at ?: now(),
            ]);
        }

        // 4. Disputes logs
        $disputes = DB::table('disputes')->get();
        foreach ($disputes as $dispute) {
            DB::table('audit_logs')->insert([
                'category' => 'dispute',
                'user' => $dispute->party_one_name . ' (Sponsor)',
                'action' => 'Opened Dispute Case ' . $dispute->id . ' against ' . $dispute->party_two_name . ' regarding ' . $dispute->reason,
                'ip_address' => '192.168.29.' . rand(10, 250),
                'created_at' => $dispute->created_at ?: now(),
                'updated_at' => $dispute->updated_at ?: now(),
            ]);

            if ($dispute->status === 'Resolved') {
                DB::table('audit_logs')->insert([
                    'category' => 'admin_action',
                    'user' => 'SuperAdmin (support@marketplace.com)',
                    'action' => 'Dispute Case ' . $dispute->id . ' RESOLVED & CLOSED by Mediator. Verdict: ' . ($dispute->admin_notes ?: 'Case Settled'),
                    'ip_address' => '192.168.1.1',
                    'created_at' => $dispute->updated_at ?: now(),
                    'updated_at' => $dispute->updated_at ?: now(),
                ]);
            }
        }

        // 5. Moderation Reports logs
        $reports = DB::table('moderation_reports')->get();
        foreach ($reports as $report) {
            DB::table('audit_logs')->insert([
                'category' => 'verification',
                'user' => $report->reported_by_name . ' (Sponsor)',
                'action' => 'Submitted Safety Report ' . $report->id . ' on ' . $report->reported_user_name . ' (' . $report->reason . ')',
                'ip_address' => '94.200.12.' . rand(10, 250),
                'created_at' => $report->created_at ?: now(),
                'updated_at' => $report->created_at ?: now(),
            ]);
        }
    }

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