<?php

namespace Modules\Inventory\Services;

use App\Models\ActivityLogModel;
use App\Models\ProductBrandModel;

class BrandService
{
    protected ProductBrandModel $brands;
    protected ActivityLogModel $activity;

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

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

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

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

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

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

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

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

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

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

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

        return ['success' => true];
    }

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

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

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

        $this->brands->delete($id);
        $this->activity->log('brands', 'deleted', $actingUserId, "Deleted brand #{$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',
        ];
    }
}
