<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Estimate\CreateEstimateRequest;
use App\Http\Requests\Estimate\UpdateEstimateRequest;
use App\Services\Estimate\EstimateService;
use Illuminate\Http\Request;

class EstimateController extends Controller
{
    protected EstimateService $estimateService;

    public function __construct(EstimateService $estimateService)
    {
        $this->estimateService = $estimateService;
    }

    public function index(Request $request)
    {
        $estimates = $this->estimateService->getEstimates($request->all());

        return ApiResponse::success([
            'estimates' => $estimates,
        ], __('message.fetched'));
    }

    public function stats()
    {
        $stats = $this->estimateService->stats();

        return ApiResponse::success([
            'stats' => $stats,
        ], __('message.fetched'));
    }

    public function show($id)
    {
        $estimate = $this->estimateService->getEstimateDetail($id);

        return ApiResponse::success([
            'estimate' => $estimate,
        ], __('message.fetched'));
    }

    public function store(CreateEstimateRequest $request)
    {
        $estimate = $this->estimateService->createEstimate($request->validated());

        return ApiResponse::success([
            'estimate' => $estimate,
        ], __('message.created'), 201);
    }

    public function update(UpdateEstimateRequest $request, $id)
    {
        $estimate = $this->estimateService->updateEstimate($id, $request->validated());

        return ApiResponse::success([
            'estimate' => $estimate,
        ], __('message.updated'));
    }

    public function destroy($id)
    {
        $this->estimateService->deleteEstimate($id);

        return ApiResponse::success([], __('message.deleted'));
    }

    public function clone(Request $request, $id)
    {
        $request->validate([
            'estimate_name' => 'required|string|max:255',
            'customer_name' => 'nullable|string|max:255',
            'project_name' => 'nullable|string|max:255',
            'notes' => 'nullable|string|max:2000',
        ]);

        $newEstimate = $this->estimateService->cloneEstimate($id, $request->all());

        return ApiResponse::success([
            'estimate' => $newEstimate,
        ], __('message.estimate.cloned'), 201);
    }

    public function changeStatus(Request $request, $id)
    {
        $request->validate([
            'status' => 'required|in:DRAFT,SENT,APPROVED,REJECTED,EXPIRED',
            'notes' => 'nullable|string|max:1000',
        ]);

        $estimate = $this->estimateService->changeEstimateStatus(
            $id,
            $request->status,
            $request->notes
        );

        return ApiResponse::success([
            'estimate' => $estimate,
        ], __('message.estimate.status_changed'));
    }

    public function getFormSchema($modelSetId)
    {
        $schema = $this->estimateService->getEstimateFormSchema($modelSetId);

        return ApiResponse::success([
            'form_schema' => $schema,
        ], __('message.fetched'));
    }

    public function previewCalculation(Request $request, $modelSetId)
    {
        $request->validate([
            'parameters' => 'required|array',
            'parameters.*' => 'required',
        ]);

        $calculation = $this->estimateService->previewCalculation(
            $modelSetId,
            $request->parameters
        );

        return ApiResponse::success([
            'calculation' => $calculation,
        ], __('message.calculated'));
    }
}
