<?php

namespace Database\Seeders\Demo;

use App\Models\Branch;
use App\Models\Sales\Till;
use Illuminate\Database\Seeder;

/**
 * Seeds the branch network + tills every other Demo\* seeder builds on.
 */
class BranchSeeder extends Seeder
{
    /** @var array<string,array{city:string,phone:string}> */
    public const BRANCHES = [
        'Colombo City Store' => ['city' => 'Colombo', 'phone' => '0112345678'],
        'Kandy Branch' => ['city' => 'Kandy', 'phone' => '0812234567'],
        'Galle Branch' => ['city' => 'Galle', 'phone' => '0912345678'],
    ];

    public function run(): void
    {
        foreach (self::BRANCHES as $name => $meta) {
            $branch = Branch::firstOrCreate(
                ['name' => $name],
                [
                    'phone' => $meta['phone'],
                    'email' => strtolower(str_replace(' ', '.', $name)).'@vellix.lk',
                    'address' => "No. {$this->randomHouseNo()}, Main Street",
                    'city' => $meta['city'],
                    'country' => 'Sri Lanka',
                    'is_active' => true,
                ],
            );

            Till::firstOrCreate(
                ['identifier' => strtoupper(substr($meta['city'], 0, 3)).'-TILL-01'],
                [
                    'name' => "{$meta['city']} Front Counter",
                    'branch_id' => $branch->id,
                    'is_active' => true,
                ],
            );

            Till::firstOrCreate(
                ['identifier' => strtoupper(substr($meta['city'], 0, 3)).'-TILL-02'],
                [
                    'name' => "{$meta['city']} Mobile Sales Device",
                    'branch_id' => $branch->id,
                    'is_active' => true,
                ],
            );
        }
    }

    private function randomHouseNo(): int
    {
        return random_int(10, 250);
    }
}
