<?php

namespace Modules\Leave\Services;

use App\Models\ActivityLogModel;
use App\Models\LeaveTypeModel;

class LeaveTypeService
{
    protected LeaveTypeModel $types;
    protected ActivityLogModel $activity;

    public function __construct()
    {
        $this->types    = new LeaveTypeModel();
        $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 leave 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('leave_types', 'created', $actingUserId, "Created leave 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' => 'Leave type not found.']];
        }

        $code = strtoupper(trim($input['code'] ?? ''));
        if ($this->types->isCodeTaken($code, $id)) {
            return ['success' => false, 'errors' => ['code' => 'That leave 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('leave_types', 'updated', $actingUserId, "Updated leave 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('leave_types', 'activated', $actingUserId, "Activated leave type #{$id}", (string) $id);
    }

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

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

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

        return ['success' => true];
    }

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