<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

final class PlayTypeSeeder extends Seeder
{
    public function run(): void
    {
        $defaults = ['created_at' => now(), 'updated_at' => now()];

        foreach ($this->rows() as $row) {
            DB::table('play_types')->updateOrInsert(
                ['play_code' => $row['play_code']],
                array_merge($row, $defaults),
            );
        }
    }

    /**
     * @return list<array<string, mixed>>
     */
    private function rows(): array
    {
        return [
            $this->row('big', 'standard', 4, 'single', 'Big', 10),
            $this->row('small', 'standard', 4, 'single', 'Small', 20),

            $this->row('pos_4a', 'position', 4, 'single', '4A', 30, false, ['prize_scope' => ['first']]),
            $this->row('pos_4b', 'position', 4, 'single', '4B', 40, false, ['prize_scope' => ['second']]),
            $this->row('pos_4c', 'position', 4, 'single', '4C', 50, false, ['prize_scope' => ['third']]),
            $this->row('pos_4d', 'position', 4, 'single', '4D', 60, false, ['prize_scope' => ['starter']]),
            $this->row('pos_4e', 'position', 4, 'single', '4E', 70, false, ['prize_scope' => ['consolation']]),

            $this->row('pos_3a', 'position', 3, 'single', '3A', 80, false, ['prize_scope' => ['first']]),
            $this->row('pos_3b', 'position', 3, 'single', '3B', 90, false, ['prize_scope' => ['second']]),
            $this->row('pos_3c', 'position', 3, 'single', '3C', 100, false, ['prize_scope' => ['third']]),
            $this->row('pos_3abc', 'position', 3, 'single', '3ABC', 110, false, ['prize_scope' => ['first', 'second', 'third']]),

            $this->row('pos_2a', 'position', 2, 'single', '2A', 120, false, ['prize_scope' => ['first']]),
            $this->row('pos_2b', 'position', 2, 'single', '2B', 130, false, ['prize_scope' => ['second']]),
            $this->row('pos_2c', 'position', 2, 'single', '2C', 140, false, ['prize_scope' => ['third']]),
            $this->row('pos_2abc', 'position', 2, 'single', '2ABC', 150, false, ['prize_scope' => ['first', 'second', 'third']]),

            $this->row('straight', 'box', 4, 'single', 'Straight', 160, false, ['expand_mode' => 'straight']),
            $this->row('box', 'box', 4, 'single', 'Box', 170, false, ['expand_mode' => 'box']),
            $this->row('ibox', 'box', 4, 'per_combination', 'iBox', 180, true, ['expand_mode' => 'box']),
            $this->row('mbox', 'box', 4, 'shared_total', 'mBox', 190, true, ['expand_mode' => 'box']),
            $this->row('roll', 'box', 4, 'per_combination', 'Roll', 200, true, ['expand_mode' => 'roll']),
            $this->row('half_box', 'box', 4, 'single', 'Half Box', 210, true, ['reserved' => true], false),

            $this->row('head', 'attribute', 4, 'single', 'Head', 220, false, ['attribute' => 'head']),
            $this->row('tail', 'attribute', 4, 'single', 'Tail', 230, false, ['attribute' => 'tail']),
            $this->row('odd', 'attribute', null, 'single', 'Odd', 240, false, ['attribute' => 'odd']),
            $this->row('even', 'attribute', null, 'single', 'Even', 250, false, ['attribute' => 'even']),
            $this->row('digit_big', 'attribute', null, 'single', 'Big Digit', 260, false, ['attribute' => 'digit_big']),
            $this->row('digit_small', 'attribute', null, 'single', 'Small Digit', 270, false, ['attribute' => 'digit_small']),
        ];
    }

    /**
     * @return array<string, mixed>
     */
    private function row(
        string $playCode,
        string $category,
        ?int $dimension,
        ?string $betMode,
        string $name,
        int $sortOrder,
        bool $supportsMultiNumber = false,
        ?array $rules = null,
        bool $isEnabled = true,
    ): array {
        return [
            'play_code' => $playCode,
            'category' => $category,
            'dimension' => $dimension,
            'bet_mode' => $betMode,
            'display_name' => $name,
            'is_enabled' => $isEnabled,
            'sort_order' => $sortOrder,
            'supports_multi_number' => $supportsMultiNumber,
            'reserved_rule_json' => $rules === null ? null : json_encode($rules, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
        ];
    }
}
