<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateBetBuilderTables extends Migration
{
    public function up()
    {
        // builder_matches table: admin-curated pool of selectable matches.
        // our_prediction is kept hidden from clients until a ticket that includes
        // this match has been paid.
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'team1' => [
                'type' => 'VARCHAR',
                'constraint' => 120,
            ],
            'team2' => [
                'type' => 'VARCHAR',
                'constraint' => 120,
            ],
            'league' => [
                'type' => 'VARCHAR',
                'constraint' => 120,
                'null' => true,
            ],
            'match_time' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'odds' => [
                'type' => 'DECIMAL',
                'constraint' => '6,2',
                'default' => 1.00,
            ],
            'our_prediction' => [
                'type' => 'VARCHAR',
                'constraint' => 120,
                'null' => true,
            ],
            'status' => [
                'type' => 'ENUM',
                'constraint' => ['upcoming', 'settled', 'void'],
                'default' => 'upcoming',
            ],
            'result_outcome' => [
                'type' => 'ENUM',
                'constraint' => ['pending', 'correct', 'wrong'],
                'default' => 'pending',
            ],
            'is_archived' => [
                '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('match_time');
        $this->forge->addKey('is_archived');
        $this->forge->addKey('status');
        $this->forge->createTable('builder_matches');

        // builder_tickets table: a user's selected legs + computed combined odds
        // + tier price. legs_snapshot freezes the odds/teams at create-time so
        // later admin edits can't change a user's paid ticket.
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'msisdn' => [
                'type' => 'VARCHAR',
                'constraint' => 80,
            ],
            'match_ids' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'legs_snapshot' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'combined_odds' => [
                'type' => 'DECIMAL',
                'constraint' => '12,4',
                'default' => 1.0000,
            ],
            'leg_count' => [
                'type' => 'INT',
                'constraint' => 11,
                'default' => 0,
            ],
            'amount' => [
                'type' => 'INT',
                'constraint' => 11,
                'default' => 0,
            ],
            'transaction_id' => [
                'type' => 'VARCHAR',
                'constraint' => 80,
                'null' => true,
            ],
            'status' => [
                'type' => 'ENUM',
                'constraint' => ['pending', 'paid', 'settled', 'void'],
                'default' => 'pending',
            ],
            '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->addKey('status');
        $this->forge->createTable('builder_tickets');
    }

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