<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateOfflineBillsAndNotifications extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'token' => ['type' => 'VARCHAR', 'constraint' => 80],
            'user_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'shop_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'discount_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'amount' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'default' => 0],
            'discount_amount' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'default' => 0],
            'payable' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'default' => 0],
            'reward_points' => ['type' => 'INT', 'constraint' => 11, 'default' => 0],
            'status' => ['type' => 'VARCHAR', 'constraint' => 20, 'default' => 'PENDING'],
            'approved_by' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'approved_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(['token'], false, true);
        $this->forge->addKey(['user_id']);
        $this->forge->addKey(['shop_id']);
        $this->forge->addKey(['status']);
        $this->forge->createTable('rewarder_offline_bill_requests', true);

        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'user_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'shop_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'type' => ['type' => 'VARCHAR', 'constraint' => 50, 'default' => 'general'],
            'title' => ['type' => 'VARCHAR', 'constraint' => 120, 'default' => ''],
            'body' => ['type' => 'TEXT', 'null' => true],
            'action_url' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'entity_type' => ['type' => 'VARCHAR', 'constraint' => 50, 'null' => true],
            'entity_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'is_read' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'read_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['user_id']);
        $this->forge->addKey(['shop_id']);
        $this->forge->addKey(['is_read']);
        $this->forge->addKey(['created_at']);
        $this->forge->createTable('rewarder_notifications', true);
    }

    public function down()
    {
        $this->forge->dropTable('rewarder_notifications', true);
        $this->forge->dropTable('rewarder_offline_bill_requests', true);
    }
}

