<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAttendanceCommentTemplates extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('attendance_comment_template')) {
            return;
        }

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'min_score' => [
                'type'       => 'DECIMAL',
                'constraint' => '5,2',
                'null'       => false,
            ],
            'max_score' => [
                'type'       => 'DECIMAL',
                'constraint' => '5,2',
                'null'       => false,
            ],
            'template_text' => [
                'type' => 'TEXT',
                'null' => false,
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 1,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['min_score', 'max_score']);
        $this->forge->addKey('is_active');
        $this->forge->createTable('attendance_comment_template', true);

        $now = date('Y-m-d H:i:s');
        $rows = [
            [
                'min_score'    => 100.00,
                'max_score'    => 100.00,
                'template_text' => '{name} has maintained perfect attendance this semester, showing exceptional commitment and reliability. Outstanding consistency in attending every class session without fail. Keep it up and stay strong - bravo!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 95.00,
                'max_score'    => 99.99,
                'template_text' => '{name} has maintained excellent attendance this semester, demonstrating strong commitment and reliability. Only rare absences, with a consistent routine of being present for class. Keep up the great consistency - well done!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 90.00,
                'max_score'    => 94.99,
                'template_text' => '{name} has demonstrated very good attendance this semester, reflecting dependable responsibility. Missed only a few class sessions and remains consistent overall. Keep striving for even stronger attendance - great work!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 85.00,
                'max_score'    => 89.99,
                'template_text' => '{name} has maintained good attendance this semester and displays responsible habits. A few absences were noted, yet overall consistency is solid. Aim for even fewer missed days to strengthen your routine - keep it going!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 80.00,
                'max_score'    => 84.99,
                'template_text' => '{name}\'s attendance has been fairly consistent this semester, with some missed class sessions. Being present more regularly will support steady progress. You\'re on the right track - keep improving!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 75.00,
                'max_score'    => 79.99,
                'template_text' => '{name}\'s attendance has been moderate this semester, with several absences impacting consistency. Prioritizing attendance will help strengthen routines and lead to success. Let\'s keep improving week by week - you\'ve got this!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 70.00,
                'max_score'    => 74.99,
                'template_text' => '{name}\'s attendance has been somewhat inconsistent this semester, with missed sessions creating noticeable gaps. Focusing on regular attendance will help build a steady and reliable routine. Keep working on it - you can do it!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 65.00,
                'max_score'    => 69.99,
                'template_text' => '{name}\'s attendance needs improvement this semester due to several missed classes. A renewed focus on being present regularly will help build stronger habits and lead to a successful finish. Keep going - you\'ve got this!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 60.00,
                'max_score'    => 64.99,
                'template_text' => '{name}\'s attendance has been a concern this semester, with many missed classes. Making a consistent effort to attend regularly will help strengthen routines and keep learning on track. Stay focused - you can do this!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 55.00,
                'max_score'    => 59.99,
                'template_text' => '{name}\'s attendance has been challenging this semester, with numerous absences recorded. A stronger commitment to being present each week will help build better habits and steady progress. Keep pushing forward!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 50.00,
                'max_score'    => 54.99,
                'template_text' => '{name}\'s attendance has been inconsistent this semester, with many missed sessions. Prioritizing daily attendance will help rebuild consistency, strengthen learning routines, and support steady progress. You\'ve got this - stay committed!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 45.00,
                'max_score'    => 49.99,
                'template_text' => '{name}\'s attendance has been irregular this semester, with several missed classes. Refocusing on regular attendance will help restore consistency and lay the foundation for success. Keep at it - you\'re capable!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
            [
                'min_score'    => 0.00,
                'max_score'    => 44.99,
                'template_text' => '{name}\'s attendance has been significantly limited this semester, with many absences. Starting fresh with consistent attendance can help you rebuild strong habits and finish the term successfully. A new start - let\'s go!',
                'is_active'    => 1,
                'created_at'   => $now,
                'updated_at'   => $now,
            ],
        ];

        $this->db->table('attendance_comment_template')->insertBatch($rows);
    }

    public function down()
    {
        if (!$this->db->tableExists('attendance_comment_template')) {
            return;
        }

        $this->forge->dropTable('attendance_comment_template', true);
    }
}
