<?php
declare(strict_types=1);

namespace App\Modules\Shipments;

use App\Core\Http\Request;
use App\Core\Http\Response;

final class ShipmentPresetController
{
    public function __construct(
        private readonly ShipmentPresetRepository $repository
    ) {
    }

    public function list(Request $request): Response
    {
        return Response::json(['presets' => $this->repository->findAll()]);
    }

    public function store(Request $request): Response
    {
        $name = trim((string) $request->input('name', ''));
        if ($name === '') {
            return Response::json(['error' => 'Name is required'], 422);
        }

        $data = $this->extractData($request);
        $id = $this->repository->create($data);

        $preset = $this->repository->findById($id);

        return Response::json(['preset' => $preset], 201);
    }

    public function update(Request $request): Response
    {
        $id = (int) $request->input('id', '0');
        if ($id <= 0) {
            return Response::json(['error' => 'Invalid preset ID'], 400);
        }

        $existing = $this->repository->findById($id);
        if ($existing === null) {
            return Response::json(['error' => 'Preset not found'], 404);
        }

        $data = $this->extractData($request);
        if (trim($data['name']) === '') {
            return Response::json(['error' => 'Name is required'], 422);
        }

        $this->repository->update($id, $data);

        $preset = $this->repository->findById($id);

        return Response::json(['preset' => $preset]);
    }

    public function destroy(Request $request): Response
    {
        $id = (int) $request->input('id', '0');
        if ($id <= 0) {
            return Response::json(['error' => 'Invalid preset ID'], 400);
        }

        $existing = $this->repository->findById($id);
        if ($existing === null) {
            return Response::json(['error' => 'Preset not found'], 404);
        }

        $this->repository->delete($id);

        return Response::json(['deleted' => true]);
    }

    /**
     * @return array<string, mixed>
     */
    private function extractData(Request $request): array
    {
        return [
            'name' => (string) $request->input('name', ''),
            'color' => (string) $request->input('color', '#3b82f6'),
            'carrier' => (string) $request->input('carrier', ''),
            'provider_code' => (string) $request->input('provider_code', ''),
            'delivery_method_id' => (string) $request->input('delivery_method_id', ''),
            'credentials_id' => (string) $request->input('credentials_id', ''),
            'carrier_id' => (string) $request->input('carrier_id', ''),
            'package_type' => (string) $request->input('package_type', 'PACKAGE'),
            'length_cm' => (string) $request->input('length_cm', '25'),
            'width_cm' => (string) $request->input('width_cm', '20'),
            'height_cm' => (string) $request->input('height_cm', '8'),
            'weight_kg' => (string) $request->input('weight_kg', '1'),
            'sender_point_id' => (string) $request->input('sender_point_id', ''),
            'label_format' => (string) $request->input('label_format', 'PDF'),
            'sort_order' => (string) $request->input('sort_order', '0'),
        ];
    }
}
