<?php

namespace Modules\Documents\Services;

use App\Models\ActivityLogModel;
use App\Models\DocumentTypeModel;

class DocumentTypeService
{
    protected DocumentTypeModel $types;
    protected ActivityLogModel $activity;

    public function __construct()
    {
        $this->types    = new DocumentTypeModel();
        $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 document type code is already in use.']];
        }

        $data = [
            'name'            => trim($input['name'] ?? ''),
            'code'            => $code,
            'requires_expiry' => ! empty($input['requires_expiry']) ? 1 : 0,
            'requires_number' => ! empty($input['requires_number']) ? 1 : 0,
            'description'     => $input['description'] ?: null,
            'status'          => 'active',
            'created_by'      => $actingUserId,
            'updated_by'      => $actingUserId,
        ];

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

        $id = $this->types->getInsertID();
        $this->activity->log('document_types', 'created', $actingUserId, "Created document 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' => 'Document type not found.']];
        }

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

        $data = [
            'name'            => trim($input['name'] ?? $type['name']),
            'code'            => $code,
            'requires_expiry' => ! empty($input['requires_expiry']) ? 1 : 0,
            'requires_number' => ! empty($input['requires_number']) ? 1 : 0,
            'description'     => $input['description'] ?: null,
            'updated_by'      => $actingUserId,
        ];

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

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

        return ['success' => true];
    }

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

        $this->types->update($id, ['status' => $status, 'updated_by' => $actingUserId]);
        $this->activity->log('document_types', $status, $actingUserId, "Set document type #{$id} to {$status}", (string) $id);

        return ['success' => true];
    }

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

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

        return ['success' => true];
    }
}
