<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTaskOccurrences extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'task_id' => [
                'type'     => 'BIGINT',
                'unsigned' => true,
                'null'     => false,
            ],
            'occurrence_date' => [
                'type' => 'DATE',
                'null' => true,
            ],
            'occurrence_time' => [
                'type' => 'TIME',
                'null' => true,
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['pending', 'completed'],
                'null'       => false,
                'default'    => 'pending',
            ],
            'completed_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'is_deleted' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'unsigned'   => true,
                'null'       => false,
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey(['task_id', 'occurrence_date']);
        $this->forge->addKey(['status', 'is_deleted']);
        $this->forge->addForeignKey('task_id', 'tasks', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('task_occurrences', true);
    }

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