<?php

namespace App\Http\Controllers\Admin;

use App\DataTables\ReportsRevenueDataTable;
use App\Http\Controllers\Controller;
use App\Jobs\ReportExportJob;
use App\Services\ReportsService;
use Illuminate\Http\Request;

class ReportsController extends Controller
{
    protected ReportsService $service;

    public function __construct(ReportsService $service)
    {
        $this->service = $service;

        $this->middleware('can:reports.view')->only(['index', 'revenueData']);
        $this->middleware('can:reports.export')->only(['exportRevenue']);
    }

    public function index()
    {
        return view('admin.reports');
    }

    public function revenueData(ReportsRevenueDataTable $dataTable)
    {
        return $dataTable->ajax();
    }

    public function exportRevenue(Request $request)
    {
        $from = $request->input('from');
        $to = $request->input('to');
        $filename = 'revenue_'.now()->format('YmdHis').'.csv';

        // Dispatch job to export in background
        $job = new ReportExportJob('revenue', ['from' => $from, 'to' => $to, 'filename' => $filename]);
        dispatch($job);

        return response()->json(['success' => true, 'message' => 'Export started. You will be notified when complete.']);
    }
}
