<?php

namespace App\Database\Seeds;

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

class ChatSeeder extends Seeder
{
    public function run()
    {
        $data = [
            [
                "name" => "_produccion",
                "display" => "Producción",
                "users" => [
                    "mbalbaci@safekat.com",
                    "mari.cano@safekat.com",
                    "beatriz@safekat.com",
                    "imnavajas@coit.es",
                ],
            ],
            [
                "name" => "_pod",
                "display" => "POD",
                "users" => [
                    "pod@safekat.com",
                    "imnavajas@coit.es",
                ],
            ],
            [
                "name" => "_maquetacion",
                "display" => "Maquetación",
                "users" => [
                    "maquetacion@safekat.com",
                    "imnavajas@coit.es",
                ],
            ],
            // [
            //     "name" => "_comercial",
            //     "display" => "Comercial",
            //     "users" => [
            //         "incidencias@safekat.com",
            //     ],
            // ],
            [
                "name" => "_incidencias",
                "display" => "Incidencias",
                "users" => [
                    "incidencias@safekat.com",
                ],
            ],
            [
                "name" => "_logistica",
                "display" => "Logística",
                "users" => [
                    "logistica@safekat.com",
                    "imnavajas@coit.es",
                ],
            ],
            [
                "name" => "_admin",
                "display" => "Administración",
                "users" => [
                    "contabilidad@safekat.com",
                    "imnavajas@coit.es",
                ],
            ],
        ];
        $chatDeparmentModel = model(ChatDeparmentModel::class);
        $chatDeparmentUsersModel = model(ChatDeparmentUserModel::class);
        $userModel = model(UserModel::class);
        foreach ($data as $row) {
            $chatDeparmentId = $chatDeparmentModel->insert(["name" => $row["name"], "display" => $row["display"]]);
            if (count($row["users"]) > 0) {
                foreach ($row["users"] as $mail) {
                    $user = $userModel->like("username", explode("@",$mail)[0])->first();
                    
                    if ($user) {
                        echo $user->id."\r\n";
                        $chatDeparmentUsersModel->insert(['user_id' => $user->id, "chat_department_id" => $chatDeparmentId]);
                    }
                }
            }
        };
    }
}
