<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use CodeIgniter\Database\RawSql;

class VisitorMigration extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'first_name' => [
                'type' => 'VARCHAR',
                'constraint' => 32
            ],
            'last_name' => [
                'type' => 'VARCHAR',
                'constraint' => 32
            ],
            'gender' => [
                'type' => 'ENUM',
                'constraint' => ['Male', 'Female']
            ],
            'phone' => [
                'type' => 'VARCHAR',
                'constraint' => 10
            ],
            'coming_from' => [
                'type' => 'VARCHAR',
                'constraint' => 100
            ],
            'going_to' => [
                'type' => 'VARCHAR',
                'constraint' => 100
            ],
            'purpose' => [
                'type' => 'int',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'status' => [
                'type' => 'ENUM',
                'constraint' => ['In', 'Out', 'Expired'],
                'default' => 'In'
            ],
            'signature' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
            ],
            'signout_at' => [
                'type' => 'DATETIME',
                'default' => null,
            ],
            'time_taken' => [
                'type' => 'int',
                'default' => null,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'default' => new RawSql('current_timestamp')
            ],
            'updated_at DATETIME DEFAULT current_timestamp on update current_timestamp NULL',
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('purpose', 'tbl_purpose', 'id');
        $this->forge->createTable('tbl_visitor', false);
    }

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