<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateDeliveryNotesTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'folio' => ['type' => 'VARCHAR', 'constraint' => 40],
            'template_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'client_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'title' => ['type' => 'VARCHAR', 'constraint' => 200],
            'body_html' => ['type' => 'LONGTEXT'],
            'field_values_json' => ['type' => 'LONGTEXT', 'null' => true],
            'status' => ['type' => "ENUM('draft','signed','delivered')", 'default' => 'draft'],
            'verify_token' => ['type' => 'CHAR', 'constraint' => 32],
            'signed_at' => ['type' => 'DATETIME', 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('folio');
        $this->forge->addUniqueKey('verify_token');
        $this->forge->addKey('template_id');
        $this->forge->addKey('client_id');
        $this->forge->addForeignKey('template_id', 'delivery_note_templates', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('client_id', 'clients', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('delivery_notes');
    }

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