<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateBadgePrintLogs extends Migration
{
    public function up()
    {
        // Defensive: only create if not exists
        if ($this->db->tableExists('badge_print_logs')) {
            return;
        }

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'user_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
            ],
            'printed_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => true,
            ],
            'school_year' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
            ],
            'printed_at' => [
                'type' => 'DATETIME',
                'null' => false,
            ],
            'role' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'class_section_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 191,
                'null'       => true,
            ],
            'copies' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
                'default'    => 1,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['user_id']);
        $this->forge->addKey(['school_year']);
        $this->forge->createTable('badge_print_logs', true);
    }

    public function down()
    {
        if ($this->db->tableExists('badge_print_logs')) {
            $this->forge->dropTable('badge_print_logs', true);
        }
    }
}
