<?php

namespace Database\Seeders;

use App\Models\SupportTicket;
use App\Models\SupportTicketReply;
use App\Models\User;
use App\Models\Worker;
use Illuminate\Database\Seeder;

class SupportTicketSeeder extends Seeder
{
    public function run(): void
    {
        // Get primary employer user
        $employer = User::where('role', 'employer')->first();
        // Get primary worker
        $worker = Worker::first();
        // Get admin user
        $admin = User::where('role', 'admin')->first();

        if (!$employer || !$admin) {
            return;
        }

        // 1. High priority billing issue (Open)
        $ticket1 = SupportTicket::create([
            'ticket_number' => 'TKT-782103',
            'user_id' => $employer->id,
            'subject' => 'PayTabs throws 500 error on premium upgrade',
            'description' => 'I attempted to pay for the Premium Sponsor Pass (199 AED). After completing my 3D-Secure card verification, the screen redirected to a white page with a 500 Internal Server Error status. Can you check if my credit card was debited?',
            'status' => 'open',
            'priority' => 'high',
        ]);

        // 2. Performance issue (Resolved)
        $ticket2 = SupportTicket::create([
            'ticket_number' => 'TKT-910482',
            'user_id' => $employer->id,
            'subject' => 'Worker search is lagging when applying multiple filters',
            'description' => 'When I search for candidates under category "Housekeeping" and filter by area "Dubai Marina" and experience "5+ Years", the browser loading spinner hangs for 4-5 seconds before displaying any candidates.',
            'status' => 'resolved',
            'priority' => 'medium',
        ]);

        // Add reply history to ticket 2
        SupportTicketReply::create([
            'support_ticket_id' => $ticket2->id,
            'user_id' => $admin->id,
            'message' => 'Hello. I have passed this ticket to our senior systems engineering team to profile the search query lifecycle. They will inspect the index scan timings on the workers table.',
            'is_developer_response' => false,
        ]);

        SupportTicketReply::create([
            'support_ticket_id' => $ticket2->id,
            'user_id' => $admin->id,
            'message' => "Hello. I analyzed the database query performance profile on the route you accessed. It was doing an expensive sequential scan on a growing table because of a missing multi-column composite index. I've written and executed a migration to inject the missing index and set up a Redis caching layer for the query output with a 300-second TTL. The response latency has dropped from 4.8s to 35ms under load. Please reload the dashboard and let me know if it feels significantly snappier.",
            'is_developer_response' => true, // Mark as Senior Software Developer response!
        ]);

        SupportTicketReply::create([
            'support_ticket_id' => $ticket2->id,
            'user_id' => $employer->id,
            'message' => 'Wow! Yes, the search page is loading almost instantaneously now. The performance improvement is huge. Thanks for the quick engineering turnaround. You can close this ticket now.',
            'is_developer_response' => false,
        ]);

        // 3. Worker issue (Open)
        if ($worker) {
            SupportTicket::create([
                'ticket_number' => 'TKT-551048',
                'worker_id' => $worker->id,
                'subject' => 'Unable to upload passport copy document',
                'description' => 'Every time I try to upload my scanned passport copy in jpeg format, the app says "OCR reading accuracy limit failed". The image is extremely clear and high-resolution. Please approve my profile manually.',
                'status' => 'open',
                'priority' => 'medium',
            ]);
        }
    }
}
