<?php

namespace App\Models;

class RewarderStateModel extends BaseModel
{
    protected $table = 'rewarder_states';
    protected $primaryKey = 'id';
    protected $returnType = 'object';
    protected $allowedFields = ['country_id', 'name', 'status', 'created_at', 'updated_at'];

    public function activeWithCountry()
    {
        return $this->select('rewarder_states.*, rewarder_countries.name AS country_name')
            ->join('rewarder_countries', 'rewarder_countries.id = rewarder_states.country_id', 'left')
            ->where('rewarder_states.status', 1)
            ->orderBy('rewarder_countries.name', 'asc')
            ->orderBy('rewarder_states.name', 'asc')
            ->findAll();
    }

    public function activeOptionList()
    {
        $rows = $this->select('rewarder_states.id, rewarder_states.name, rewarder_countries.name AS country_name')
            ->join('rewarder_countries', 'rewarder_countries.id = rewarder_states.country_id', 'left')
            ->where('rewarder_states.status', 1)
            ->orderBy('rewarder_countries.name', 'asc')
            ->orderBy('rewarder_states.name', 'asc')
            ->findAll();

        return array_map(static function ($r) {
            return (object) [
                'id' => (int) $r->id,
                'label' => trim($r->name . ' - ' . ($r->country_name ?? '')),
            ];
        }, $rows);
    }
}

