<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateSpinwheels extends Migration
{
    public function up()
    {
        // rewarder_spinwheels
        $this->forge->addField([
            'id'         => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'title'      => ['type' => 'VARCHAR', 'constraint' => 150],
            'description'=> ['type' => 'TEXT', 'null' => true],
            'status'     => ['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->createTable('rewarder_spinwheels', true);
        $this->forge->reset();

        // rewarder_spinwheel_slices
        $this->forge->addField([
            'id'                => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'spinwheel_id'      => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'label'             => ['type' => 'VARCHAR', 'constraint' => 150],
            'reward_value'      => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'picture_type'      => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
            'spin_cost'         => ['type' => 'DECIMAL', 'constraint' => '10,2', 'default' => 0.00],
            'probability_weight'=> ['type' => 'INT', 'constraint' => 11, 'default' => 0],
            'color'             => ['type' => 'VARCHAR', 'constraint' => 7, 'default' => '#FF0000'],
            'status'            => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'sort_order'        => ['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(['spinwheel_id']);
        $this->forge->createTable('rewarder_spinwheel_slices', true);
        $this->forge->reset();

        // Seed permissions for Spinwheels
        $perms = [
            ['title' => 'Spinwheels List',   'code' => 'spinwheels_list'],
            ['title' => 'Add Spinwheels',    'code' => 'spinwheels_add'],
            ['title' => 'Edit Spinwheels',   'code' => 'spinwheels_edit'],
            ['title' => 'Delete Spinwheels', 'code' => 'spinwheels_delete'],
        ];

        foreach ($perms as $p) {
            $exists = $this->db->table('permissions')->where('code', $p['code'])->countAllResults();
            if (!$exists) {
                $this->db->table('permissions')->insert($p);
            }
        }

        $adminRole = $this->db->table('roles')->select('id')->where('title', 'Admin')->get()->getRow();
        if ($adminRole) {
            foreach ($perms as $p) {
                $exists = $this->db->table('role_permissions')
                    ->where(['role' => $adminRole->id, 'permission' => $p['code']])
                    ->countAllResults();
                if (!$exists) {
                    $this->db->table('role_permissions')->insert(['role' => $adminRole->id, 'permission' => $p['code']]);
                }
            }
        }
    }

    public function down()
    {
        $this->forge->dropTable('rewarder_spinwheel_slices', true);
        $this->forge->dropTable('rewarder_spinwheels', true);
        // Note: permissions are not removed in down()
    }
}

