<?php
namespace Incoviba\Model\Proyecto\Broker;

use Incoviba\Common;
use Incoviba\Model;

class Contract extends Common\Ideal\Model
{
    public Model\Proyecto $project;
    public Model\Proyecto\Broker $broker;
    public float $commission;
    protected ?array $states;

    public function states(): ?array
    {
        if (!isset($this->states) or count($this->states) === 0) {
            $this->states = $this->runFactory('states');
        }
        return $this->states;
    }

    protected ?Contract\State $current;
    public function current(): ?Contract\State
    {
        if (!isset($this->current)) {
            try {
                $this->current = last($this->states());
            } catch (\TypeError $error) {
                $this->current = null;
            }
        }
        return $this->current;
    }

    protected array $promotions = [];
    public function promotions(): array
    {
        if (count($this->promotions) === 0) {
            $this->promotions = $this->runFactory('promotions');
        }
        return $this->promotions;
    }

    protected function jsonComplement(): array
    {
        return [
            'project_id' => $this->project->id,
            'broker_rut' => $this->broker->rut,
            'commission' => $this->commission,
            'states' => $this->states(),
            'current' => $this->current(),
        ];
    }
}
