<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateMessaging extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                  => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'job_application_id'  => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'created_at'          => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('job_application_id', 'job_applications', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('conversations', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'conversation_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'user_id'         => ['type' => 'BIGINT', 'unsigned' => true],
        ]);
        $this->forge->addPrimaryKey(['conversation_id', 'user_id']);
        $this->forge->addForeignKey('conversation_id', 'conversations', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('conversation_participants', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'              => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'conversation_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'sender_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'body'            => ['type' => 'TEXT'],
            'is_read'         => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'read_at'         => ['type' => 'TIMESTAMP', 'null' => true],
            'created_at'      => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['conversation_id', 'created_at']);
        $this->forge->addForeignKey('conversation_id', 'conversations', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('sender_id', 'users', 'id', false, 'RESTRICT');
        $this->forge->createTable('messages', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'message_id'         => ['type' => 'BIGINT', 'unsigned' => true],
            'file_path'          => ['type' => 'VARCHAR', 'constraint' => 255],
            'original_filename'  => ['type' => 'VARCHAR', 'constraint' => 255],
            'file_size_kb'       => ['type' => 'INT', 'unsigned' => true, 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('message_id', 'messages', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('message_attachments', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('message_attachments', true);
        $this->forge->dropTable('messages', true);
        $this->forge->dropTable('conversation_participants', true);
        $this->forge->dropTable('conversations', true);
    }
}
