<?php

namespace App\Services;

use App\Enums\EstimateStatus;
use App\Models\Estimates\MainRequestEstimate;
use App\Repositories\MainRequestRepository;

class EstimateService
{
    protected $mainRequestRepo;

    public function __construct(MainRequestRepository $mainRequestRepo)
    {
        $this->mainRequestRepo = $mainRequestRepo;
    }

    /**
     * 견적 신규 등록
     */
    public function createEstimate($mainRequestId, array $data, $actorId): MainRequestEstimate
    {
        $estimate = $this->mainRequestRepo->createEstimate($mainRequestId, $data);

        // 상태 이력 기록 (draft)
        $this->mainRequestRepo->addFlow(
            $mainRequestId,
            get_class($estimate),
            $estimate->id,
            EstimateStatus::Draft->value,
            '등록',
            $actorId,
            '견적서 임시 등록'
        );

        return $estimate;
    }

    /**
     * 견적 상태 변경
     */
    public function changeEstimateStatus(MainRequestEstimate $estimate, EstimateStatus $newStatus, $actorId, $memo = null)
    {
        $estimate->status_code = $newStatus;
        $estimate->save();

        // 이력 기록
        $this->mainRequestRepo->addFlow(
            $estimate->main_request_id,
            get_class($estimate),
            $estimate->id,
            $newStatus->value,
            '상태변경',
            $actorId,
            $memo
        );
    }

    // ... 기타 견적 비즈니스 로직도 여기에!
}
