<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTicketsSystem extends Migration
{
    public function up()
    {
        // Tabla de Categorías
        $this->forge->addField([
            'id'        => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'keyword'    => ['type' => 'VARCHAR', 'constraint' => 100, 'unique' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->createTable('tickets_categorias');

        // Tabla de secciones
        $this->forge->addField([
            'id'        => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'keyword'    => ['type' => 'VARCHAR', 'constraint' => 100, 'unique' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->createTable('tickets_secciones');

        // Tabla de Estados
        $this->forge->addField([
            'id'        => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'keyword'    => ['type' => 'VARCHAR', 'constraint' => 100, 'unique' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->createTable('tickets_estados');

        // Tabla de Tickets
        $this->forge->addField([
            'id'            => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'usuario_id'    => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'user_soporte_id'    => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'categoria_id'  => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'seccion_id'     => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'estado_id'     => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'default' => 1],
            'prioridad'     => ['type' => 'ENUM', 'constraint' => ['alta', 'media', 'baja'], 'default' => 'media'],
            'titulo'        => ['type' => 'VARCHAR', 'constraint' => 255],
            'descripcion'   => ['type' => 'TEXT'],
            'created_at'    => ['type' => 'DATETIME', 'null' => true],
            'updated_at'    => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('usuario_id', 'users', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->addForeignKey('user_soporte_id', 'users', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->addForeignKey('categoria_id', 'tickets_categorias', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->addForeignKey('seccion_id', 'tickets_secciones', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->addForeignKey('estado_id', 'tickets_estados', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->createTable('tickets');

        // Tabla de Respuestas
        $this->forge->addField([
            'id'         => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'ticket_id'  => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'usuario_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'mensaje'    => ['type' => 'TEXT'],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('ticket_id', 'tickets', 'id', '', 'NO ACTION');
        $this->forge->addForeignKey('usuario_id', 'users', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->createTable('tickets_respuestas');

        // Tabla de Adjuntos
        $this->forge->addField([
            'id'         => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'ticket_id'  => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'respuesta_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'nombre'    => ['type' => 'VARCHAR', 'constraint' => 255],
            'hash'    => ['type' => 'VARCHAR', 'constraint' => 255],
            'path'    => ['type' => 'VARCHAR', 'constraint' => 255],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('ticket_id', 'tickets', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->addForeignKey('respuesta_id', 'tickets_respuestas', 'id', 'NO ACTION', 'NO ACTION');
        $this->forge->createTable('tickets_adjuntos');
    }

    public function down()
    {
        $this->forge->dropTable('tickets_adjuntos');
        $this->forge->dropTable('tickets_respuestas');
        $this->forge->dropTable('tickets');
        $this->forge->dropTable('tickets_categorias');
        $this->forge->dropTable('tickets_estados');
        $this->forge->dropTable('tickets_secciones');

        $this->db->table('config_variables_app')->where('name', 'default_soporte_user_id')->delete();
    }
}
