<?php

namespace Modules\Assets\Services;

use App\Models\ActivityLogModel;
use App\Models\AssetTypeModel;

class AssetTypeService
{
    protected AssetTypeModel $types;
    protected ActivityLogModel $activity;

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

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

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

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

        $id = $this->types->getInsertID();
        $this->activity->log('asset_types', 'created', $actingUserId, "Created asset type #{$id} ({$data['name']})", (string) $id);

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

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

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

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

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

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

        return ['success' => true];
    }

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

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

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

        $this->types->delete($id);
        $this->activity->log('asset_types', 'deleted', $actingUserId, "Deleted asset type #{$id}", (string) $id);

        return ['success' => true];
    }

    protected function mapInput(array $input, ?int $actingUserId): array
    {
        return [
            'name'                => trim($input['name']),
            'category'            => trim($input['category'] ?? '') ?: null,
            'requires_identifier' => ! empty($input['requires_identifier']) ? 1 : 0,
            'is_consumable'       => ! empty($input['is_consumable']) ? 1 : 0,
            'requires_return'     => ! empty($input['requires_return']) ? 1 : 0,
            'description'         => $input['description'] ?: null,
            'status'              => $input['status'] ?? 'active',
        ];
    }
}
