<?php

namespace App\Services;

use App\Enums\AgentStatus;
use App\Enums\CommissionType;
use App\Models\Agent;
use App\Models\User;

class AgentService
{
    /**
     * @param  array{
     *     name: string,
     *     company_name: string|null,
     *     phone: string,
     *     email: string|null,
     *     address: string|null,
     *     commission_type: CommissionType|null,
     *     commission_value: string|null,
     *     status: AgentStatus
     * }  $data
     */
    public function create(User $actor, array $data): Agent
    {
        return Agent::query()->create([
            ...$data,
            'created_by' => $actor->id,
        ]);
    }

    /**
     * @param  array{
     *     name?: string,
     *     company_name?: string|null,
     *     phone?: string,
     *     email?: string|null,
     *     address?: string|null,
     *     commission_type?: CommissionType|null,
     *     commission_value?: string|null,
     *     status?: AgentStatus
     * }  $data
     */
    public function update(Agent $agent, array $data): Agent
    {
        $agent->update($data);

        return $agent->refresh();
    }

    public function delete(Agent $agent): void
    {
        $agent->delete();
    }
}
