<?php

namespace App\Database\Seeds;

use App\Models\Chat\ChatDeparmentModel;
use App\Models\Chat\ChatDeparmentUserModel;
use App\Models\Chat\ChatMessageModel;
use App\Models\Chat\ChatModel;
use App\Models\ChatNotification;
use CodeIgniter\Database\Seeder;
use App\Models\Usuarios\UserModel;

class MessageSeeder extends Seeder
{
    public function run()
    {

        $data = [
                "title" => "Message Test",
                "messages" => [
                    [
                        "sender_id" => 1,
                        "receiver_id" => 127,
                    ],
                    [
                        "sender_id" => 127,
                        "receiver_id" => 1,
                    ]
                ]
        ];
        $chatModel = model(ChatModel::class);
        $chatMessageModel = model(ChatMessageModel::class);
        $chatNotificationModel = model(ChatNotification::class);
        $userModel = model(UserModel::class);
        foreach (range(1,100) as $key => $value) {
            $chat_id = $chatModel->insert(["title" => $data["title"]." ".$value]);
            foreach ($data["messages"] as $key => $value) {
                $first_name = $userModel->find($value["receiver_id"])?->first_name;
                $chat_message_id = $chatMessageModel->insert([
                    "chat_id" => $chat_id,
                    "sender_id" => $value["sender_id"],
                    "receiver_id" => $value["receiver_id"],
                    "message" => "Hola"." ".$first_name
                ]);
                $chatNotificationModel->insert([
                    "chat_message_id" => $chat_message_id,
                    "user_id" => $value["receiver_id"]
                ]);
            }
        }
    }
}
