<?php

namespace App\Modules\Admin;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\BaseController;

class AdminControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        // Check if user is admin
        if (!Auth::user() || Auth::user()->role !== 'admin') {
            return Response::json(['error' => 'Unauthorized'], 403);
        }

        $query = DB::table('users')
            ->select('users.*')
            ->orderBy('users.created_at', 'desc');

        $query = $query->paginate(request()->get('limit', 20));

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

        return Response::json($response);
    }

    /**
     * Get system statistics
     *
     * @return Response
     */
    public function getStats()
    {
        // Check if user is admin
        if (!Auth::user() || Auth::user()->role !== 'admin') {
            return Response::json(['error' => 'Unauthorized'], 403);
        }

        $stats = [
            'total_users' => DB::table('users')->count(),
            'total_tasks' => DB::table('tasks')->count(),
            'total_clients' => DB::table('clients')->count(),
            'active_tasks' => DB::table('tasks')->where('completed', 0)->count(),
            'completed_tasks' => DB::table('tasks')->where('completed', 1)->count(),
        ];

        return Response::json($stats);
    }

    /**
     * Get system logs
     *
     * @return Response
     */
    public function getLogs()
    {
        // Check if user is admin
        if (!Auth::user() || Auth::user()->role !== 'admin') {
            return Response::json(['error' => 'Unauthorized'], 403);
        }

        $query = DB::table('activity_logs')
            ->select('activity_logs.*', 'users.first_name', 'users.last_name')
            ->leftJoin('users', 'users.id', '=', 'activity_logs.user_id')
            ->orderBy('activity_logs.created_at', 'desc');

        if (request()->get('user_id')) {
            $query->where('activity_logs.user_id', request()->get('user_id'));
        }

        if (request()->get('action')) {
            $query->where('activity_logs.action', request()->get('action'));
        }

        $query = $query->paginate(request()->get('limit', 50));

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

        return Response::json($response);
    }
} 