<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateDocuments extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'title' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
            'author_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'comment'    => 'Cached from user_profiles.name at submission time',
            ],
            'author_user_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'comment'    => 'NULL if admin-uploaded without student association',
            ],
            'nim' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => true,
                'comment'    => 'NIM at time of submission',
            ],
            'study_program_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'document_type_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'year' => [
                'type'       => 'YEAR',
            ],
            'abstract' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'keywords' => [
                'type'       => 'VARCHAR',
                'constraint' => 500,
                'null'       => true,
                'comment'    => 'Comma-separated keywords',
            ],
            'supervisor' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
                'comment'    => 'Dosen pembimbing',
            ],
            'publisher' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
                'comment'    => 'For journals and proceedings',
            ],
            'cover_img' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
                'comment'    => 'UUID filename in storage/covers/',
            ],
            'file_path' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'comment'    => 'UUID filename in storage/pdf/',
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['draft', 'submitted', 'review', 'revision', 'published', 'rejected'],
                'default'    => 'draft',
            ],
            'admin_note' => [
                'type' => 'TEXT',
                'null' => true,
                'comment' => 'Feedback from admin for revision or rejection',
            ],
            'citations_count' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'default'    => 0,
                'comment'    => 'Internal citations only — denormalized for performance',
            ],
            'view_count' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'default'    => 0,
            ],
            'download_count' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'default'    => 0,
            ],
            'submitted_at' => ['type' => 'DATETIME', 'null' => true],
            'reviewed_at'  => ['type' => 'DATETIME', 'null' => true],
            'published_at' => ['type' => 'DATETIME', 'null' => true],
            'created_at'   => ['type' => 'DATETIME', 'null' => true],
            'updated_at'   => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('status');
        $this->forge->addKey('document_type_id');
        $this->forge->addKey('author_user_id');
        $this->forge->addKey('study_program_id');
        $this->forge->addForeignKey('author_user_id', 'users', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('document_type_id', 'document_types', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('study_program_id', 'study_programs', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('documents');
    }

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