<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Str;

class OmniBoardSeeder extends Seeder
{
    public function run()
    {
        // 1. Criar o Cliente
        $tenantId = DB::table('tenants')->insertGetId([
            'name' => 'LinePBX Demo',
            'api_key' => Str::random(32),
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        // 2. Criar as Filas (Baseado na sua imagem)
        $queues = [
            ['name' => 'Central de Atendimento Voz New', 'type' => 'voice'],
            ['name' => 'Unific Central de Atendimento WEB', 'type' => 'whatsapp'],
            ['name' => 'Unific Coleta Domiciliar WEB', 'type' => 'whatsapp'],
            ['name' => 'Unific Autorizacao WEB', 'type' => 'whatsapp'],
            ['name' => 'Imagem', 'type' => 'voice'],
        ];

        foreach ($queues as $q) {
            $queueId = DB::table('queues')->insertGetId([
                'tenant_id' => $tenantId,
                'name' => $q['name'],
                'type' => $q['type'],
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            // 3. Simular Métricas do Dia (Para preencher os cards)
            // Vou gerar números aleatórios mas realistas para cada fila
            $received = rand(50, 600);
            $answered = intval($received * rand(60, 95) / 100);
            $abandoned = $received - $answered;
            $sla = rand(20, 98); // SLA variado para testar cores (verde/vermelho)

            DB::table('daily_metrics')->insert([
                'queue_id' => $queueId,
                'date' => Carbon::today(),
                'received_count' => $received,
                'answered_count' => $answered,
                'abandoned_count' => $abandoned,
                'sla_percentage' => $sla,
                'avg_wait_time' => rand(60, 300), // 1 a 5 min
                'avg_talk_time' => rand(180, 600), // 3 a 10 min
                'max_wait_time' => rand(600, 3000), // Picos altos
                'transferred_count' => rand(0, 10),
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            // 4. Povoar a Lista de Espera (Sidebar "Próximos Atendimentos")
            // Adiciona 3 a 5 pessoas esperando em cada fila
            for ($i = 0; $i < rand(3, 5); $i++) {
                DB::table('waiting_list')->insert([
                    'queue_id' => $queueId,
                    'caller_name' => fake()->firstName() . ' ' . fake()->lastName(),
                    'caller_number' => fake()->phoneNumber(),
                    'entered_at' => Carbon::now()->subMinutes(rand(1, 45)), // Entrou entre 1 e 45 min atrás
                    'attempt_count' => rand(1, 3),
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
        
        // 5. Criar Agentes
        $statuses = ['available', 'talking', 'paused', 'offline'];
        for ($i=0; $i < 10; $i++) { 
             DB::table('agents')->insert([
                'tenant_id' => $tenantId,
                'name' => fake()->name(),
                'extension' => rand(1000, 9999),
                'status' => $statuses[array_rand($statuses)],
                'last_status_change' => now()->subMinutes(rand(5, 120)),
                'created_at' => now(),
                'updated_at' => now(),
             ]);
        }
    }
}