<?php

namespace App\Database\Migrations\Panel;

use CodeIgniter\Database\Migration;

class Conversations extends Migration
{
    public function up()
    {

        $fields = [
            'id' => ['type' => 'varchar', 'constraint' => 255],
            'group_type' => ['type' => 'enum', 'constraint' => "'private','group'"],
            'group_name' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];

        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->createTable('group_conversations', true);

        $fields = [
            'id' => ['type' => 'varchar', 'constraint' => 255],
            'gc_id' => ['type' => 'varchar', 'constraint' => 255],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];

        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('gc_id', 'group_conversations', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('group_conversations_member', true);

        $fields = [
            'id' => ['type' => 'varchar', 'constraint' => 255],
            'gc_id' => ['type' => 'varchar', 'constraint' => 255],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'message' => ['type' => 'text'],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];

        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('gc_id', 'group_conversations', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('panel_conversations', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('group_conversations_member', 'group_conversations_member_user_id_foreign');
            $this->forge->dropForeignKey('group_conversations_member', 'group_conversations_member_gc_id_foreign');
            $this->forge->dropForeignKey('panel_conversations', 'panel_conversations_user_id_foreign');
            $this->forge->dropForeignKey('panel_conversations', 'panel_conversations_gc_id_foreign');
        }
        $this->forge->dropTable('panel_conversations', true);
        $this->forge->dropTable('group_conversations_member', true);
        $this->forge->dropTable('group_conversations', true);
    }
}
