<?php

namespace App\Modules\Stats;

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

class StatsControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $query = DB::table('stats')
            ->select('stats.*')
            ->orderBy('stats.created_at', 'desc');

        // Apply filters
        if (request()->get('type')) {
            $query->where('stats.type', request()->get('type'));
        }

        if (request()->get('date_from')) {
            $query->where('stats.date', '>=', request()->get('date_from'));
        }

        if (request()->get('date_to')) {
            $query->where('stats.date', '<=', request()->get('date_to'));
        }

        $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);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $data = request()->all();
        
        $statId = DB::table('stats')->insertGetId([
            'type' => $data['type'],
            'value' => $data['value'],
            'date' => $data['date'] ?? now()->toDateString(),
            'description' => $data['description'] ?? '',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return Response::json(['success' => true, 'stat_id' => $statId]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $stat = DB::table('stats')->where('id', $id)->first();

        if (!$stat) {
            return Response::json(['error' => 'Stat not found'], 404);
        }

        return Response::json($stat);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $stat = DB::table('stats')->where('id', $id)->first();

        if (!$stat) {
            return Response::json(['error' => 'Stat not found'], 404);
        }

        $data = request()->all();
        $data['updated_at'] = now();

        DB::table('stats')->where('id', $id)->update($data);

        return Response::json(['success' => true, 'message' => 'Stat updated']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $stat = DB::table('stats')->where('id', $id)->first();

        if (!$stat) {
            return Response::json(['error' => 'Stat not found'], 404);
        }

        DB::table('stats')->where('id', $id)->delete();

        return Response::json(['success' => true, 'message' => 'Stat deleted']);
    }

    /**
     * Get dashboard statistics
     *
     * @return Response
     */
    public function getDashboardStats()
    {
        $stats = [
            'total_users' => DB::table('users')->count(),
            'total_tasks' => DB::table('tasks')->count(),
            'total_clients' => DB::table('clients')->count(),
            'total_invoices' => DB::table('invoices')->count(),
            'active_tasks' => DB::table('tasks')->where('completed', 0)->count(),
            'completed_tasks' => DB::table('tasks')->where('completed', 1)->count(),
            'pending_invoices' => DB::table('invoices')->where('status', 'pending')->count(),
            'paid_invoices' => DB::table('invoices')->where('status', 'paid')->count(),
        ];

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