<?php

namespace App\Entities\Chat;

use App\Entities\Facturas\FacturaEntity;
use App\Entities\Pedidos\PedidoEntity;
use App\Entities\Presupuestos\PresupuestoEntity;
use App\Entities\Produccion\OrdenTrabajoEntity;
use App\Entities\Usuarios\UserEntity;
use App\Models\Chat\ChatDeparmentModel;
use App\Models\Chat\ChatMessageModel;
use App\Models\ChatUser;
use App\Models\Facturas\FacturaModel;
use App\Models\OrdenTrabajo\OrdenTrabajoModel;
use App\Models\Pedidos\PedidoModel;
use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Model;
use stdClass;

class ChatEntity extends Entity
{
    protected $attributes = [
        "id" => null,
        "chat_department_id" => null,
        "pedido_id" => null,
        "presupuesto_id" => null,
        "factura_id" => null,
        "orden_trabajo_id" => null,
        "title" => null
    ];


    protected $casts = [
        "chat_department_id" => "?integer",
        "pedido_id" => "?integer",
        "presupuesto_id" => "?integer",
        "factura_id" => "?integer",
        "orden_trabajo_id" => "?integer",
        "title" => "string"
    ];

    public function withMessages(): self
    {
        $auth_user = auth()->user();
        $messages = $this->messages();
        foreach ($messages as $key => $message) {
            if ($message->sender_id == $auth_user->id) {
                $message->pos = 'right';
            } else {
                $message->pos = 'left';
            }
        }
        $this->attributes["messages"] = $messages;
        return $this;
    }
    public function department(): ?ChatDepartmentEntity
    {
        $m = model(ChatDeparmentModel::class);
        return $m->find($this->attributes['chat_department_id']);
    }
    public function presupuesto(): ?PresupuestoEntity
    {
        $m = model(PresupuestoModel::class);
        return $m->find($this->attributes['presupuesto_id']);
    }
    public function pedido(): ?PedidoEntity
    {
        $m = model(PedidoModel::class);
        return $m->find($this->attributes['pedido_id']);
    }
    public function factura(): ?FacturaEntity
    {
        $m = model(FacturaEntity::class);
        return $m->find($this->attributes['factura_id']);
    }
    public function orden_trabajo(): ?OrdenTrabajoEntity
    {
        $m = model(OrdenTrabajoModel::class);
        return $m->find($this->attributes['orden_trabajo_id']);
    }
    public function messages(): ?array
    {
        $m = model(ChatMessageModel::class);

        $messages = $m->where('chat_id', $this->attributes["id"])->findAll();
        return array_map(fn(ChatMessageEntity $m) => $m->withUser(), $messages);
    }
    public function withHebra(): self
    {
        $this->attributes["messages"] = array_map(fn(ChatMessageEntity $m) => $m->withUser(), $this->messages());
        $this->attributes["internalUsers"] = array_map(fn(ChatUserEntity $u) => $u->user(), $this->internalUsers());
        return $this;
    }
    /**
     * Undocumented function
     *
     * @return array<ChatUserEntity>
     */
    public function internalUsers(): array
    {
        $m = model(ChatUser::class);
        return $m->where('chat_id', $this->attributes["id"])->findAll() ?? [];
    }
    public function getModel(): ?Model
    {
        $model = null;
        $models = [
            "presupuesto_id" => model(PresupuestoModel::class),
            "pedido_id" => model(PedidoModel::class),
            "factura_id" => model(FacturaModel::class),
            "orden_trabajo_id" => model(OrdenTrabajoModel::class),
        ];
        $fks = [
            "presupuesto_id" => $this->attributes["presupuesto_id"],
            "pedido_id" => $this->attributes["pedido_id"],
            "factura_id" => $this->attributes["factura_id"],
            "orden_trabajo_id" => $this->attributes["orden_trabajo"],
        ];
        foreach ($fks as $key => $fk) {
            if ($fk) {
                $model = $models[$key];
                break;
            }
        }
        return $model;
    }
    public function relatedFk(): array
    {
        $relatedFk = null;
        $relatedFkValue = null;
        $fks = [
            "presupuesto" => $this->attributes["presupuesto_id"],
            "pedido" => $this->attributes["pedido_id"],
            "factura" => $this->attributes["factura_id"],
            "ot" => $this->attributes["orden_trabajo_id"],
        ];
        foreach ($fks as $key => $fk) {
            if ($fk) {
                $relatedFk = $key;
                $relatedFkValue = $fk;
                break;
            }
        }
        return [
            "relatedModel" => $relatedFk,
            "relatedFkValue" => $relatedFkValue
        ];
    }
    public function withModel() : self
    {
        if($this->getModel()){
            $this->attributes["modelData"] = $this->getModel()->find($this->relatedFk()['relatedFkValue']);
        }else{
            $this->attributes["modelData"] = null;
        }
        return $this;
    }
}
