<?php

namespace Modules\Inventory\Services;

use App\Models\ActivityLogModel;
use App\Models\UnitOfMeasureModel;

class UnitOfMeasureService
{
    protected UnitOfMeasureModel $units;
    protected ActivityLogModel $activity;

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

    public function create(array $input, ?int $actingUserId): array
    {
        $code = strtoupper(trim($input['code'] ?? ''));
        if ($this->units->isCodeTaken($code)) {
            return ['success' => false, 'errors' => ['code' => 'That unit code is already in use.']];
        }

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

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

        $id = $this->units->getInsertID();
        $this->activity->log('units_of_measure', 'created', $actingUserId, "Created unit #{$id} ({$data['name']})", (string) $id);

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

    public function update(int $id, array $input, ?int $actingUserId): array
    {
        $unit = $this->units->find($id);
        if (! $unit) {
            return ['success' => false, 'errors' => ['id' => 'Unit of measure not found.']];
        }

        $code = strtoupper(trim($input['code'] ?? ''));
        if ($this->units->isCodeTaken($code, $id)) {
            return ['success' => false, 'errors' => ['code' => 'That unit code is already in use.']];
        }

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

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

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

        return ['success' => true];
    }

    public function activate(int $id, ?int $actingUserId): void
    {
        $this->units->update($id, ['status' => 'active', 'updated_by' => $actingUserId]);
        $this->activity->log('units_of_measure', 'activated', $actingUserId, "Activated unit #{$id}", (string) $id);
    }

    public function deactivate(int $id, ?int $actingUserId): void
    {
        $this->units->update($id, ['status' => 'inactive', 'updated_by' => $actingUserId]);
        $this->activity->log('units_of_measure', 'deactivated', $actingUserId, "Deactivated unit #{$id}", (string) $id);
    }

    public function delete(int $id, ?int $actingUserId): array
    {
        $unit = $this->units->find($id);
        if (! $unit) {
            return ['success' => false, 'error' => 'Unit of measure not found.'];
        }

        $this->units->delete($id);
        $this->activity->log('units_of_measure', 'deleted', $actingUserId, "Deleted unit #{$id}", (string) $id);

        return ['success' => true];
    }

    protected function mapInput(array $input): array
    {
        return [
            'name'        => trim($input['name']),
            'symbol'      => trim($input['symbol'] ?? '') ?: null,
            'description' => $input['description'] ?: null,
            'status'      => $input['status'] ?? 'active',
        ];
    }
}
