<?php

namespace Modules\Inventory\Services;

use App\Models\ActivityLogModel;
use App\Models\ProductCategoryModel;

class CategoryService
{
    protected ProductCategoryModel $categories;
    protected ActivityLogModel $activity;

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

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

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

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

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

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

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

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

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

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

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

        return ['success' => true];
    }

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

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

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

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

        return ['success' => true];
    }

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