<?php
namespace App\Models;

class CommissionRule
{
    public $id;
    public $title;
    public $kpi_key;
    public $kpi_source;
    public $target_users;
    public $kpi_min_threshold;
    public $kpi_max_threshold;
    public $base_amount;
    public $reward_percentage;
    public $status;
    public $created_at;
    public $updated_at;

    public function __construct(array $data = [])
    {
        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
    }

    public function getTargetUsersArray(): array
    {
        if (is_array($this->target_users)) {
            return $this->target_users;
        }
        $decoded = json_decode((string)$this->target_users, true);
        return is_array($decoded) ? $decoded : [];
    }

    public function setTargetUsersArray(array $users): void
    {
        $this->target_users = json_encode(array_values($users), JSON_UNESCAPED_UNICODE);
    }

    public function toArray(): array
    {
        return get_object_vars($this);
    }
}
