<?php

namespace Database\Seeders;

use App\Models\Comment;
use App\Models\SupportCategory;
use App\Models\Ticket;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        foreach ([
            ['name' => 'General support', 'sort_order' => 1],
            ['name' => 'Technical support', 'sort_order' => 2],
            ['name' => 'Sales', 'sort_order' => 3],
        ] as $category) {
            SupportCategory::firstOrCreate(
                ['name' => $category['name']],
                [
                    'slug' => \Illuminate\Support\Str::slug($category['name']),
                    'is_active' => true,
                    'sort_order' => $category['sort_order'],
                ]
            );
        }

        $admin = User::updateOrCreate(
            ['email' => 'ogwubenvic18@gmail.com'],
            [
                'name' => 'Admin User',
                'password' => Hash::make('1234567890'),
                'role' => 'admin',
                'is_super_admin' => true,
                'department' => 'IT',
                'category' => null,
            ]
        );

        $agent = User::updateOrCreate(
            ['email' => 'agent@example.com'],
            [
                'name' => 'Support Agent',
                'password' => Hash::make('password'),
                'role' => 'agent',
                'is_super_admin' => false,
                'department' => 'Support',
                'category' => 'Technical support',
            ]
        );

        $salesAgent = User::updateOrCreate(
            ['email' => 'sales@example.com'],
            [
                'name' => 'Sales Agent',
                'password' => Hash::make('password'),
                'role' => 'agent',
                'is_super_admin' => false,
                'department' => 'Sales',
                'category' => 'Sales',
            ]
        );

        $user = User::updateOrCreate(
            ['email' => 'user@example.com'],
            [
                'name' => 'Regular User',
                'password' => Hash::make('password'),
                'role' => 'user',
                'is_super_admin' => false,
                'department' => 'General',
                'category' => null,
            ]
        );

        User::query()
            ->where('role', 'agent')
            ->where(function ($query) {
                $query->whereNull('category')
                    ->orWhere('category', '')
                    ->orWhere('category', 'Technical Support');
            })
            ->update([
                'department' => 'Support',
                'category' => 'Technical support',
            ]);

        User::query()
            ->where('role', 'user')
            ->where(function ($query) {
                $query->whereNull('department')->orWhere('department', '');
            })
            ->update([
                'department' => 'General',
            ]);

        $ticket = Ticket::firstOrCreate(
            ['id' => 'MT-123456'],
            [
                'title' => 'Cannot access email',
                'description' => 'User cannot log into email account',
                'category' => 'Technical support',
                'priority' => 'High',
                'status' => 'open',
                'requester' => $user->id,
                'requester_id' => $user->id,
                'requester_name' => $user->name,
                'department' => 'HR',
                'assigned_to_id' => $agent->id,
                'sla_due' => now()->addHours(12),
                'first_response_sla' => now()->addHour(),
                'resolution_sla' => now()->addHours(12),
            ]
        );

        Comment::firstOrCreate(
            ['ticket_id' => $ticket->id, 'author_id' => $agent->id, 'content' => "We're looking into this issue."],
            ['is_internal' => false, 'is_first_response' => true]
        );
    }
}
