<?php

namespace App\Services;

use App\Models\Claim\Claim;
use App\Models\Claim\Status;

class ClaimService {
    public function updateStatus(Claim $claim, int $status_id, HistoryService $service): bool {

        if ($status_id === $claim->status_id) {
            return false;
        }

        $status = Status::findOrFail($status_id);

        $oldStatus = $claim->status;

        $claim->update(['status_id' => $status_id]);
        $claim->updateStatus($status_id);

        $service->add($claim->client, 'Claim status changed from "' . $oldStatus->display_name . '" to "' . $status->display_name . '"', [
            'claim_id' => $claim->id,
            'from_status_id' => $oldStatus->id,
            'from_status' => $oldStatus->display_name,
            'to_status_id' => $status_id,
            'to_status' => $status->display_name,
        ]);

        return true;
    }
}