<?php

namespace Modules\Auth\Controllers;

use App\Controllers\BaseController;
use Modules\Auth\Models\UserModel;
use Modules\Tpin\Services\TpinService;
use Modules\Users\Services\UserService;

class ProfileController extends BaseController
{
    public function index()
    {
        $userId = (int) $this->session->get('user_id');
        if ($userId <= 0) {
            return redirect()->to('/login');
        }

        $users = new UserModel();
        $service = new UserService();
        $user    = $users->find($userId);

        if (! $user) {
            return redirect()->to('/dashboard')->with('error', 'Profile not found.');
        }

        return view('Modules\Users\Views\show', array_merge($this->baseViewData(), [
            'title'        => 'My Profile',
            'breadcrumbs'  => ['Dashboard' => base_url('dashboard'), 'Profile'],
            'user'         => $user,
            'isOnline'     => $service->isOnline($userId),
            'isOwnProfile' => true,
            'tpinStatus'   => (new TpinService())->statusFor($userId),
        ]));
    }
}
