<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateInspectionAttachmentsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'inspection_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'item_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'file_path' => ['type' => 'VARCHAR', 'constraint' => 255],
            'mime' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'size' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('inspection_id');
        $this->forge->addForeignKey('inspection_id', 'inspections', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('item_id', 'inspection_template_items', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('inspection_attachments');
    }

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