<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddEnviosLineas extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'               => ['type' => 'INT', 'auto_increment' => true],
            'envio_id'        => ['type' => 'INT', 'unsigned' => true],
            'pedido_id'        => ['type' => 'INT', 'unsigned' => true],
            'presupuesto_id'    => ['type' => 'INT', 'unsigned' => true],
            'unidades_envio'   => ['type' => 'INT', 'default' => 0],
            'unidades_total'   => ['type' => 'INT', 'default' => 0],
            'cajas'            => ['type' => 'INT', 'default' => 0],
            'unidades_cajas'   => ['type' => 'INT', 'default' => 0],
            'created_at'       => ['type' => 'DATETIME', 'null' => true],
            'updated_at'       => ['type' => 'DATETIME', 'null' => true],
            'created_by'       => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'updated_by'       => ['type' => 'INT', 'unsigned' => true, 'null' => true],
        ]);

        $this->forge->addKey('id', true); // Primary Key

        // Foreign Keys
        $this->forge->addForeignKey('presupuesto_id', 'presupuestos', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('pedido_id', 'pedidos', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('created_by', 'users', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('updated_by', 'users', 'id', 'SET NULL', 'CASCADE');

        $this->forge->createTable('envios_lineas');
    }

    public function down()
    {
        $this->forge->dropTable('envios_lineas');
    }
}
