<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\Worker;
use App\Models\Conversation;
use App\Models\Message;

class ConversationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $employer = User::where('role', 'employer')->first();
        if (!$employer) {
            return;
        }

        $workers = Worker::where('status', 'active')->take(3)->get();
        if ($workers->isEmpty()) {
            return;
        }

        // Conversation 1: with Worker 1 (John Doe)
        $w1 = $workers->get(0);
        if ($w1) {
            $conv1 = Conversation::create([
                'employer_id' => $employer->id,
                'worker_id' => $w1->id,
                'created_at' => now()->subHours(2),
                'updated_at' => now()->subHours(2),
            ]);

            Message::create([
                'conversation_id' => $conv1->id,
                'sender_type' => 'employer',
                'sender_id' => $employer->id,
                'text' => 'Hello John, I saw your profile and I am interested in hiring you as an electrician.',
                'created_at' => now()->subMinutes(110),
            ]);

            Message::create([
                'conversation_id' => $conv1->id,
                'sender_type' => 'worker',
                'sender_id' => $w1->id,
                'text' => 'Hello, thank you for reaching out! Yes, I am available.',
                'created_at' => now()->subMinutes(100),
            ]);

            Message::create([
                'conversation_id' => $conv1->id,
                'sender_type' => 'employer',
                'sender_id' => $employer->id,
                'text' => 'Great, what is your salary expectation?',
                'created_at' => now()->subMinutes(90),
            ]);

            Message::create([
                'conversation_id' => $conv1->id,
                'sender_type' => 'worker',
                'sender_id' => $w1->id,
                'text' => 'My expectation is around 2500 AED per month.',
                'created_at' => now()->subMinutes(80),
            ]);
        }

        // Conversation 2: with Worker 2 (Ali Hassan)
        $w2 = $workers->get(1);
        if ($w2) {
            $conv2 = Conversation::create([
                'employer_id' => $employer->id,
                'worker_id' => $w2->id,
                'created_at' => now()->subHour(),
                'updated_at' => now()->subHour(),
            ]);

            Message::create([
                'conversation_id' => $conv2->id,
                'sender_type' => 'employer',
                'sender_id' => $employer->id,
                'text' => 'Hi Ali, are you looking for job?',
                'created_at' => now()->subMinutes(50),
            ]);

            Message::create([
                'conversation_id' => $conv2->id,
                'sender_type' => 'worker',
                'sender_id' => $w2->id,
                'text' => 'Yes, I am available.',
                'created_at' => now()->subMinutes(45),
            ]);
        }
    }
}
