<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateInterviewUpdatesAndFiles extends Migration
{
    public function up(): void
    {
        // A CRM-style activity timeline on each interview — notes,
        // status-change entries, and file-added markers, all in one
        // chronological log visible to the whole agency team (owner +
        // recruiters), not just whoever scheduled it.
        $this->forge->addField([
            'id'            => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'interview_id'  => ['type' => 'BIGINT', 'unsigned' => true],
            'posted_by_user_id' => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true], // null for system-generated entries
            'update_type'   => ['type' => 'ENUM', 'constraint' => ['note', 'status_change', 'file_added', 'reminder_sent'], 'default' => 'note'],
            'content'       => ['type' => 'TEXT'],
            'created_at'    => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['interview_id', 'created_at']);
        $this->forge->addForeignKey('interview_id', 'interviews', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('posted_by_user_id', 'users', 'id', 'SET NULL', 'RESTRICT');
        $this->forge->createTable('interview_updates', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'                => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'interview_id'      => ['type' => 'BIGINT', 'unsigned' => true],
            'uploaded_by_user_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'file_path'         => ['type' => 'VARCHAR', 'constraint' => 255],
            'original_filename' => ['type' => 'VARCHAR', 'constraint' => 255],
            'file_size_kb'      => ['type' => 'INT', 'unsigned' => true, 'null' => true],
            'created_at'        => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('interview_id');
        $this->forge->addForeignKey('interview_id', 'interviews', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('uploaded_by_user_id', 'users', 'id', false, 'RESTRICT');
        $this->forge->createTable('interview_files', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('interview_files', true);
        $this->forge->dropTable('interview_updates', true);
    }
}
