<?php

namespace App\Http\Controllers\Api\V1\Construction;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Construction\ContractFromBiddingRequest;
use App\Http\Requests\Construction\ContractStoreRequest;
use App\Http\Requests\Construction\ContractUpdateRequest;
use App\Services\Construction\ContractService;
use Illuminate\Http\Request;

class ContractController extends Controller
{
    public function __construct(private ContractService $service) {}

    /**
     * 계약 목록 조회
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.contract.fetched'));
    }

    /**
     * 계약 상세 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.contract.fetched'));
    }

    /**
     * 계약 등록
     */
    public function store(ContractStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.contract.created'));
    }

    /**
     * 계약 수정
     */
    public function update(ContractUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->update($id, $request->validated());
        }, __('message.contract.updated'));
    }

    /**
     * 계약 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

            return 'success';
        }, __('message.contract.deleted'));
    }

    /**
     * 계약 일괄 삭제
     */
    public function bulkDestroy(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $this->service->bulkDestroy($request->input('ids', []));

            return 'success';
        }, __('message.contract.deleted'));
    }

    /**
     * 계약 통계 조회
     */
    public function stats(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->stats($request->all());
        }, __('message.contract.fetched'));
    }

    /**
     * 계약 단계별 카운트 조회
     */
    public function stageCounts(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->stageCounts($request->all());
        }, __('message.contract.fetched'));
    }

    /**
     * 입찰에서 계약 생성 (낙찰 → 계약 전환)
     */
    public function storeFromBidding(ContractFromBiddingRequest $request, int $biddingId)
    {
        return ApiResponse::handle(function () use ($request, $biddingId) {
            return $this->service->storeFromBidding($biddingId, $request->validated());
        }, __('message.contract.created'));
    }
}
