<?php

namespace Modules\Inventory\Services;

use App\Models\ActivityLogModel;
use App\Models\ProductSerialModel;

class SerialNumberService
{
    protected ProductSerialModel $serials;
    protected ActivityLogModel $activity;

    public function __construct()
    {
        $this->serials  = new ProductSerialModel();
        $this->activity = new ActivityLogModel();
    }

    public function create(array $input, ?int $actingUserId): array
    {
        $serial = trim($input['serial_number'] ?? '');
        if ($serial === '') {
            return ['success' => false, 'errors' => ['serial_number' => 'Serial number is required.']];
        }

        if ($this->serials->isSerialTaken($serial)) {
            return ['success' => false, 'errors' => ['serial_number' => 'That serial number is already in use.']];
        }

        $data = $this->mapInput($input);
        $data['serial_number'] = $serial;
        $data['created_by']    = $actingUserId;
        $data['updated_by']    = $actingUserId;

        if (! $this->serials->insert($data, true)) {
            return ['success' => false, 'errors' => $this->serials->errors()];
        }

        $id = $this->serials->getInsertID();
        $this->activity->log('serial_numbers', 'created', $actingUserId, "Created serial number #{$id} ({$serial})", (string) $id);

        return ['success' => true, 'id' => $id];
    }

    public function update(int $id, array $input, ?int $actingUserId): array
    {
        $row = $this->serials->find($id);
        if (! $row) {
            return ['success' => false, 'errors' => ['id' => 'Serial number not found.']];
        }

        $serial = trim($input['serial_number'] ?? '');
        if ($serial === '') {
            return ['success' => false, 'errors' => ['serial_number' => 'Serial number is required.']];
        }

        if ($this->serials->isSerialTaken($serial, $id)) {
            return ['success' => false, 'errors' => ['serial_number' => 'That serial number is already in use.']];
        }

        $data = $this->mapInput($input);
        $data['serial_number'] = $serial;
        $data['updated_by']    = $actingUserId;

        if (! $this->serials->update($id, $data)) {
            return ['success' => false, 'errors' => $this->serials->errors()];
        }

        $this->activity->log('serial_numbers', 'updated', $actingUserId, "Updated serial number #{$id}", (string) $id);

        return ['success' => true];
    }

    public function delete(int $id, ?int $actingUserId): array
    {
        $row = $this->serials->find($id);
        if (! $row) {
            return ['success' => false, 'error' => 'Serial number not found.'];
        }

        $this->serials->delete($id);
        $this->activity->log('serial_numbers', 'deleted', $actingUserId, "Deleted serial number #{$id}", (string) $id);

        return ['success' => true];
    }

    protected function mapInput(array $input): array
    {
        $warehouseId = $input['warehouse_id'] ?? null;

        return [
            'product_id'    => (int) ($input['product_id'] ?? 0),
            'warehouse_id'  => ($warehouseId !== null && $warehouseId !== '') ? (int) $warehouseId : null,
            'status'        => $input['status'] ?? 'in_stock',
            'notes'         => trim($input['notes'] ?? '') ?: null,
        ];
    }
}
