<?php

namespace App\Http\Controllers\Api\V1\Analytics;

use App\Http\Controllers\Controller;
use App\Services\AnalyticsService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AnalyticsController extends Controller
{
    public function __construct(
        private AnalyticsService $analyticsService,
        private ResponseService $responseService
    ) {}

    public function dashboard(Request $request): JsonResponse
    {
        $branchId = $request->input('branch_id');
        $summary = $this->analyticsService->getDashboardSummary($branchId);
        return $this->responseService->success($summary, 'Dashboard summary retrieved successfully');
    }

    public function salesReport(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $report = $this->analyticsService->getSalesReport($startDate, $endDate, $branchId);
        return $this->responseService->success($report, 'Sales report retrieved successfully');
    }

    public function expenseReport(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $report = $this->analyticsService->getExpenseReport($startDate, $endDate, $branchId);
        return $this->responseService->success($report, 'Expense report retrieved successfully');
    }

    public function profitLoss(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $report = $this->analyticsService->getProfitLossReport($startDate, $endDate, $branchId);
        return $this->responseService->success($report, 'Profit/Loss report retrieved successfully');
    }

    public function topProducts(Request $request): JsonResponse
    {
        $limit = $request->input('limit', 10);
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $products = $this->analyticsService->getTopSellingProducts($limit, $startDate, $endDate, $branchId);
        return $this->responseService->success($products, 'Top selling products retrieved successfully');
    }

    public function hourlySales(Request $request): JsonResponse
    {
        $date = $request->input('date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $sales = $this->analyticsService->getHourlySales($date, $branchId);
        return $this->responseService->success($sales, 'Hourly sales retrieved successfully');
    }

    public function salesByCategory(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $sales = $this->analyticsService->getSalesByCategory($startDate, $endDate, $branchId);
        return $this->responseService->success($sales, 'Sales by category retrieved successfully');
    }

    public function salesByPaymentMethod(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());
        $branchId = $request->input('branch_id');

        $sales = $this->analyticsService->getSalesByPaymentMethod($startDate, $endDate, $branchId);
        return $this->responseService->success($sales, 'Sales by payment method retrieved successfully');
    }

    public function branchComparison(Request $request): JsonResponse
    {
        $startDate = $request->input('start_date', now()->startOfMonth()->toDateString());
        $endDate = $request->input('end_date', now()->toDateString());

        $comparison = $this->analyticsService->getBranchComparison($startDate, $endDate);
        return $this->responseService->success($comparison, 'Branch comparison retrieved successfully');
    }

    public function inventoryValuation(Request $request): JsonResponse
    {
        $branchId = $request->input('branch_id');
        $valuation = $this->analyticsService->getInventoryValuation($branchId);
        return $this->responseService->success($valuation, 'Inventory valuation retrieved successfully');
    }

    public function lowStockAlerts(Request $request): JsonResponse
    {
        $branchId = $request->input('branch_id');
        $alerts = $this->analyticsService->getLowStockAlerts($branchId);
        return $this->responseService->success($alerts, 'Low stock alerts retrieved successfully');
    }
}
