<?php

namespace App\Database\Migrations;

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

class ChatMessages extends Migration
{
    protected array $COLUMNS = [
        "id" => [
            "type" => "INT",
            "auto_increment" => true,
        ],
        "chat_id" => [
            "type" => "INT",
            "unsigned" => true
        ],

        "sender_id" => [
            "type" => "INT",
            "unsigned" => true,
            "null" => true,

        ],
        "receiver_id" => [
            "type" => "INT",
            "unsigned" => true,
            "null" => true,
        ],
        "message" => [
            "type" => "TEXT",
        ],
        "viewed" => [
            "type" => "TEXT",
            "type" => "boolean",
            "default" => false,
        ],


    ];
    public function up()
    {
        $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(['receiver_id'], 'users', ['id']);
        $this->forge->addForeignKey(['sender_id'], 'users', ['id']);
        $this->forge->addForeignKey(['chat_id'], 'chats', ['id']);
        $this->forge->createTable("chat_messages", true);
    }

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