<?php

namespace Database\Seeders;

use App\Models\Hotel;
use App\Models\Message;
use App\Models\MessageThread;
use App\Models\ThreadParticipant;
use App\Models\User;
use App\Support\Tenancy;
use Illuminate\Database\Seeder;

class MessagingSeeder extends Seeder
{
    public function run(): void
    {
        Tenancy::set(Hotel::first());

        $manager = User::where('role', 'manager')->first();
        $chef = User::where('role', 'chef')->first();
        $housekeeper = User::where('role', 'housekeeper')->first();
        $supervisor = User::where('role', 'supervisor')->first();
        $admin = User::where('role', 'admin')->first();

        // Public community lounge
        $lounge = MessageThread::create([
            'subject' => 'Epelcon Community Lounge',
            'kind' => 'guest_chat',
            'is_public' => true,
            'created_by' => $manager?->id,
            'last_message_at' => now(),
        ]);

        $loungeMessages = [
            [$manager, 'Welcome to the Epelcon Community Lounge! 👋 Ask us anything — our team is here 24/7.'],
            [null, 'Just checked in — the room is gorgeous. Compliments to the housekeeping team!', 'Amaka N.'],
            [$housekeeper, 'Thank you Amaka! 💛 We\'re so glad you\'re enjoying your stay.'],
            [null, 'Any chance of a late checkout tomorrow? Flight is at 6pm.', 'John A.'],
            [$manager, 'Absolutely John — 3pm checkout is on the house for Gold members. See you at the desk!'],
            [null, 'The jollof rice was next level. Signed, forever a fan.', 'Grace E.'],
        ];

        $now = now()->subMinutes(count($loungeMessages) * 10);

        foreach ($loungeMessages as $line) {
            [$user, $body, $guestName] = array_pad($line, 3, null);

            Message::create([
                'message_thread_id' => $lounge->id,
                'sender_user_id' => $user?->id,
                'sender_name' => $user?->name ?? $guestName,
                'body' => $body,
                'created_at' => $now,
                'updated_at' => $now,
            ]);

            $now = $now->copy()->addMinutes(10);
        }

        $lounge->update(['last_message_at' => $now]);

        // Staff DM: manager -> chef
        if ($manager && $chef) {
            $dm1 = MessageThread::create([
                'subject' => 'Tonight\'s menu adjustment',
                'kind' => 'dm',
                'created_by' => $manager->id,
                'last_message_at' => now()->subHour(),
            ]);

            foreach ([$manager, $chef] as $u) {
                ThreadParticipant::create([
                    'message_thread_id' => $dm1->id, 'user_id' => $u->id,
                    'joined_at' => now()->subHour(), 'last_read_at' => $u->id === $manager->id ? now() : null,
                ]);
            }

            Message::create([
                'message_thread_id' => $dm1->id, 'sender_user_id' => $manager->id,
                'sender_name' => $manager->name,
                'body' => 'Kitchen inventory low on tomatoes — can we adjust tonight\'s menu? Thinking pesto pasta as tonight\'s special.',
                'created_at' => now()->subHour(), 'updated_at' => now()->subHour(),
            ]);
        }

        // Staff DM: supervisor -> housekeeper
        if ($supervisor && $housekeeper) {
            $dm2 = MessageThread::create([
                'subject' => 'Room 205 special request',
                'kind' => 'dm',
                'created_by' => $supervisor->id,
                'last_message_at' => now()->subMinutes(20),
            ]);

            foreach ([$supervisor, $housekeeper] as $u) {
                ThreadParticipant::create([
                    'message_thread_id' => $dm2->id, 'user_id' => $u->id,
                    'joined_at' => now()->subMinutes(20), 'last_read_at' => $u->id === $supervisor->id ? now() : null,
                ]);
            }

            Message::create([
                'message_thread_id' => $dm2->id, 'sender_user_id' => $supervisor->id,
                'sender_name' => $supervisor->name,
                'body' => 'Guest in 205 has requested extra pillows and a foam mattress topper. Can we set that up before 6pm?',
                'created_at' => now()->subMinutes(20), 'updated_at' => now()->subMinutes(20),
            ]);
        }

        // Broadcast announcement
        if ($admin) {
            $ann = MessageThread::create([
                'subject' => 'Grand Ballroom booking Saturday 8pm',
                'kind' => 'announcement',
                'created_by' => $admin->id,
                'last_message_at' => now()->subDays(2),
            ]);

            User::whereIn('role', array_keys(User::STAFF_ROLES))
                ->where('hotel_id', Hotel::first()->id)
                ->each(fn ($u) => ThreadParticipant::create([
                    'message_thread_id' => $ann->id, 'user_id' => $u->id,
                    'joined_at' => now()->subDays(2), 'last_read_at' => $u->id === $admin->id ? now() : null,
                ]));

            Message::create([
                'message_thread_id' => $ann->id, 'sender_user_id' => $admin->id,
                'sender_name' => $admin->name,
                'body' => 'Grand Ballroom booking Saturday 8pm — all hands on deck. Full brief in the pre-shift meeting at 3pm on the day.',
                'created_at' => now()->subDays(2), 'updated_at' => now()->subDays(2),
            ]);
        }
    }
}
