<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\SalesInvoiceResource;
use App\Services\ReportService;
use Illuminate\Http\Request;

class ReportController extends Controller
{
    protected $service;

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


    public function sales(Request $request)
    {
        $result =
            $this->service->salesReport(
                $request
            );


        return response()->json([

            'success' => true,

            'message' =>
                'Sales report fetched successfully.',

            'summary' =>
                $result['summary'],

            'data' =>
                SalesInvoiceResource::collection(
                    $result['records']
                ),

            'pagination' => [

                'current_page' =>
                    $result['records']
                        ->currentPage(),

                'last_page' =>
                    $result['records']
                        ->lastPage(),

                'per_page' =>
                    $result['records']
                        ->perPage(),

                'total' =>
                    $result['records']
                        ->total(),

            ],

        ]);
    }


    /*
    |--------------------------------------------------------------------------
    | Export Sales Report
    |--------------------------------------------------------------------------
    */

    public function exportSales(
        Request $request
    ) {

        return $this->service
            ->exportSalesReport(
                $request
            );

    }
}