<?php

namespace Modules\Departments\Services;

use App\Models\ActivityLogModel;
use App\Models\DepartmentModel;
use App\Models\EmployeeModel;

class DepartmentService
{
    protected DepartmentModel $departments;
    protected ActivityLogModel $activity;

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

    public function create(array $input, ?int $actingUserId): array
    {
        $data = [
            'branch_id'  => $input['branch_id'] ?: null,
            'name'       => $input['name'],
            'manager_id' => $input['manager_id'] ?: null,
            'status'     => $input['status'] ?? 'active',
        ];

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

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

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

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

        $data = [
            'branch_id'  => $input['branch_id'] ?: null,
            'name'       => $input['name'],
            'manager_id' => $input['manager_id'] ?: null,
        ];

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

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

        return ['success' => true];
    }

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

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

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

        $employeeCount = (new EmployeeModel())->countByDepartment($id);
        if ($employeeCount > 0) {
            return ['success' => false, 'error' => "This department has {$employeeCount} employee(s) assigned. Reassign them before deleting."];
        }

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

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