<?php
declare(strict_types=1);

namespace Controllers;

use Helpers\Request;
use Helpers\Response;
use Services\LeadService;
use Validators\Validator;

final class LeadController
{
    public function bulkOrder(): never
    {
        $data = Request::input();
        $errors = Validator::validate($data, ['name' => 'required', 'phone' => 'required|min:10', 'product_interest' => 'required', 'quantity_required' => 'required']);
        if ($errors) Response::error('Validation failed', 422, $errors);
        $id = (new LeadService())->bulk($this->bulkPayload($data));
        Response::success(['id' => $id], 'Bulk order inquiry submitted', 201);
    }

    public function distributor(): never
    {
        $data = Request::input();
        $errors = Validator::validate($data, ['name' => 'required', 'phone' => 'required|min:10', 'city' => 'required', 'business_type' => 'required']);
        if ($errors) Response::error('Validation failed', 422, $errors);
        $id = (new LeadService())->distributor($this->distributorPayload($data));
        Response::success(['id' => $id], 'Distributor inquiry submitted', 201);
    }

    public function contact(): never
    {
        $data = Request::input();
        $errors = Validator::validate($data, ['name' => 'required', 'phone' => 'required|min:10', 'message' => 'required|min:5']);
        if ($errors) Response::error('Validation failed', 422, $errors);
        $id = (new LeadService())->contact(['name'=>$data['name'],'email'=>$data['email'] ?? null,'phone'=>$data['phone'],'subject'=>$data['subject'] ?? null,'message'=>$data['message']]);
        Response::success(['id' => $id], 'Message submitted', 201);
    }

    private function bulkPayload(array $d): array { return ['name'=>$d['name'],'business_name'=>$d['business_name'] ?? null,'phone'=>$d['phone'],'email'=>$d['email'] ?? null,'city'=>$d['city'] ?? null,'product_interest'=>$d['product_interest'],'quantity_required'=>$d['quantity_required'],'message'=>$d['message'] ?? null]; }
    private function distributorPayload(array $d): array { return ['name'=>$d['name'],'business_name'=>$d['business_name'] ?? null,'phone'=>$d['phone'],'email'=>$d['email'] ?? null,'city'=>$d['city'],'state'=>$d['state'] ?? null,'business_type'=>$d['business_type'],'message'=>$d['message'] ?? null]; }
}
