<?php

namespace App\Http\Support;

class ApiInputMapper
{
    public static function emptyToNull(mixed $v): mixed
    {
        return $v === '' ? null : $v;
    }

    /** @param  array<string, mixed>  $input */
    public static function client(array $input): array
    {
        return [
            'name' => $input['name'] ?? null,
            'email' => self::emptyToNull($input['email'] ?? null),
            'company' => self::emptyToNull($input['company'] ?? null),
            'phone' => self::emptyToNull($input['phone'] ?? null),
            'address' => self::emptyToNull($input['address'] ?? null),
            'status' => $input['status'] ?? 'active',
            'notes' => self::emptyToNull($input['notes'] ?? null),
        ];
    }

    /** @param  array<string, mixed>  $input */
    public static function project(array $input): array
    {
        return [
            'name' => $input['name'] ?? null,
            'client_id' => self::emptyToNull($input['clientId'] ?? null),
            'category' => $input['category'] ?? null,
            'description' => self::emptyToNull($input['description'] ?? null),
            'price' => $input['price'] ?? 0,
            'status' => $input['status'] ?? 'planning',
            'progress' => isset($input['progress']) ? (int) $input['progress'] : 0,
            'start_date' => self::emptyToNull($input['startDate'] ?? null),
            'end_date' => self::emptyToNull($input['endDate'] ?? null),
            'deliverables' => is_array($input['deliverables'] ?? null) ? $input['deliverables'] : [],
        ];
    }

    /**
     * For PATCH-style updates (e.g. only { progress }) without nulling other columns.
     *
     * @param  array<string, mixed>  $input
     * @return array<string, mixed>
     */
    public static function projectPartial(array $input): array
    {
        $out = [];
        if (array_key_exists('name', $input)) {
            $out['name'] = $input['name'];
        }
        if (array_key_exists('clientId', $input)) {
            $out['client_id'] = self::emptyToNull($input['clientId']);
        }
        if (array_key_exists('category', $input)) {
            $out['category'] = $input['category'];
        }
        if (array_key_exists('description', $input)) {
            $out['description'] = self::emptyToNull($input['description']);
        }
        if (array_key_exists('price', $input)) {
            $out['price'] = $input['price'];
        }
        if (array_key_exists('status', $input)) {
            $out['status'] = $input['status'];
        }
        if (array_key_exists('progress', $input)) {
            $out['progress'] = (int) $input['progress'];
        }
        if (array_key_exists('startDate', $input)) {
            $out['start_date'] = self::emptyToNull($input['startDate']);
        }
        if (array_key_exists('endDate', $input)) {
            $out['end_date'] = self::emptyToNull($input['endDate']);
        }
        if (array_key_exists('deliverables', $input)) {
            $out['deliverables'] = is_array($input['deliverables']) ? $input['deliverables'] : [];
        }

        return $out;
    }

    /** @param  array<string, mixed>  $input */
    public static function invoice(array $input): array
    {
        return [
            'client_id' => self::emptyToNull($input['clientId'] ?? null),
            'date' => $input['date'] ?? null,
            'due_date' => self::emptyToNull($input['dueDate'] ?? null),
            'items' => is_array($input['items'] ?? null) ? $input['items'] : [],
            'subtotal' => $input['subtotal'] ?? 0,
            'tax' => $input['tax'] ?? 0,
            'total' => $input['total'] ?? 0,
            'status' => $input['status'] ?? 'pending',
            'notes' => self::emptyToNull($input['notes'] ?? null),
        ];
    }

    /** @param  array<string, mixed>  $input */
    public static function server(array $input): array
    {
        return [
            'name' => $input['name'] ?? null,
            'hostname' => self::emptyToNull($input['hostname'] ?? null),
            'ip_address' => self::emptyToNull($input['ipAddress'] ?? null),
            'provider' => self::emptyToNull($input['provider'] ?? null),
            'type' => self::emptyToNull($input['type'] ?? null),
            'specs' => self::emptyToNull($input['specs'] ?? null),
            'cost' => $input['cost'] ?? 0,
            'status' => $input['status'] ?? 'active',
            'renewal_date' => self::emptyToNull($input['renewalDate'] ?? null),
            'notes' => self::emptyToNull($input['notes'] ?? null),
        ];
    }

    /** @param  array<string, mixed>  $input */
    public static function domain(array $input): array
    {
        return [
            'name' => $input['name'] ?? null,
            'registrar' => self::emptyToNull($input['registrar'] ?? null),
            'expiration_date' => self::emptyToNull($input['expirationDate'] ?? null),
            'auto_renew' => (bool) ($input['autoRenew'] ?? false),
            'server_id' => self::emptyToNull($input['associatedServerId'] ?? null),
            'status' => $input['status'] ?? 'active',
            'notes' => self::emptyToNull($input['notes'] ?? null),
        ];
    }

    /** @param  array<string, mixed>  $input */
    public static function employee(array $input): array
    {
        return [
            'name' => $input['name'] ?? null,
            'email' => self::emptyToNull($input['email'] ?? null),
            'role' => $input['role'] ?? null,
            'department' => self::emptyToNull($input['department'] ?? null),
            'phone' => self::emptyToNull($input['phone'] ?? null),
            'hire_date' => self::emptyToNull($input['hireDate'] ?? null),
            'salary' => $input['salary'] ?? 0,
            'status' => $input['status'] ?? 'active',
            'notes' => self::emptyToNull($input['notes'] ?? null),
        ];
    }
}
