<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePointsTables extends Migration
{
    public function up()
    {
        // Create user_points table
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'msisdn' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
            ],
            'points' => [
                'type' => 'INT',
                'constraint' => 11,
                'default' => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('msisdn');
        $this->forge->createTable('user_points', true);

        // Create transactions table
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'msisdn' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
            ],
            'amount' => [
                'type' => 'INT',
                'constraint' => 11,
                'default' => 0,
            ],
            'transaction_id' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
            ],
            'naration' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
            ],
            'status' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
                'default' => 'pending',
            ],
            'tip_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'null' => true,
            ],
            'tip_unlocked' => [
                'type' => 'TINYINT',
                'constraint' => 1,
                'default' => 0,
            ],
            'points_used' => [
                'type' => 'INT',
                'constraint' => 11,
                'default' => 0,
            ],
            'points_awarded' => [
                'type' => 'TINYINT',
                'constraint' => 1,
                'default' => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('msisdn');
        $this->forge->addKey('transaction_id');
        $this->forge->createTable('transactions', true);

        // Create tips_unlock table
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'msisdn' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
            ],
            'tip_id' => [
                'type' => 'INT',
                'constraint' => 11,
            ],
            'transaction_id' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
            ],
            'unlock_status' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
                'default' => 'pending',
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['msisdn', 'tip_id']);
        $this->forge->createTable('tips_unlock', true);

        // Create points_usage table
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'msisdn' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
            ],
            'tip_id' => [
                'type' => 'INT',
                'constraint' => 11,
            ],
            'points_used' => [
                'type' => 'INT',
                'constraint' => 11,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['msisdn', 'tip_id']);
        $this->forge->createTable('points_usage', true);
    }

    public function down()
    {
        $this->forge->dropTable('user_points');
        $this->forge->dropTable('transactions');
        $this->forge->dropTable('tips_unlock');
        $this->forge->dropTable('points_usage');
    }
} 