<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTaskRecurrenceRules 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,
            ],
            'recurrence_type' => [
                'type'       => 'ENUM',
                'constraint' => ['daily', 'weekly', 'monthly'],
                'null'       => false,
            ],
            'interval_value' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => false,
                'default'  => 1,
            ],
            'weekly_days' => [
                'type' => 'JSON',
                'null' => true,
            ],
            'monthly_type' => [
                'type'       => 'ENUM',
                'constraint' => ['day_of_month', 'weekday_position'],
                'null'       => true,
            ],
            'monthly_day' => [
                'type' => 'TINYINT',
                'null' => true,
            ],
            'monthly_week_position' => [
                'type'       => 'ENUM',
                'constraint' => ['first', 'second', 'third', 'fourth', 'last'],
                'null'       => true,
            ],
            'monthly_weekday' => [
                'type' => 'TINYINT',
                'null' => true,
            ],
            'recurrence_time' => [
                'type' => 'TIME',
                'null' => true,
            ],
            'start_date' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'end_date' => [
                'type' => 'DATE',
                'null' => true,
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'unsigned'   => true,
                'null'       => false,
                'default'    => 1,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addKey('task_id');
        $this->forge->addForeignKey('task_id', 'tasks', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('task_recurrence_rules', true);
    }

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