<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\RawSql;
use CodeIgniter\Database\Migration;

class ChatsTable extends Migration
{
    protected array $COLUMNS = [
        "id" => [
            "type" => "INT",
            "unsigned" => true,
            "auto_increment" => true
        ],
        "chat_department_id" => [
            "type" => "INT",
            "unsigned" => true,
            "null" => true
        ],
        "pedido_id" => [
            "type" => "INT",
            "constraint" => 16,
            "unsigned" => true,
            "null" => true
        ],
        "presupuesto_id" => [
            "type" => "INT",
            "constraint" => 10,
            "unsigned" => true,
            "null" => true
        ],
        "factura_id" => [
            "type" => "INT",
            "constraint" => 10,
            "unsigned" => true,
            "null" => true
        ],


    ];
    public function up()
    {
        $this->forge->dropTable("chat_conversaciones", true);
        $this->forge->dropTable("chat_mensajes", true);
        $this->forge->dropTable("chat_participantes", true);


        $this->forge->addField($this->COLUMNS);
        $currenttime = new RawSql("CURRENT_TIMESTAMP");
        $this->forge->addField([
            "created_at" => [
                "type" => "TIMESTAMP",
                "default" => $currenttime,
            ],
            "updated_at" => [
                "type" => "TIMESTAMP",
                "null" => true,
            ],
            "deleted_at" => [
                "type" => "TIMESTAMP",
                "null" => true,

            ],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('pedido_id', 'pedidos', 'id');
        $this->forge->addForeignKey('chat_department_id', 'chat_departments', 'id');
        $this->forge->addForeignKey('factura_id', 'facturas', 'id');
        $this->forge->addForeignKey('presupuesto_id', 'presupuestos', 'id');


        $this->forge->createTable("chats", true);
    }

    public function down()
    {
        //
        $this->forge->dropTable("chats", true);
    }
}
