<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DummyChartService {
    public function getDummySalesData(Request $request)
    {
        $period = $request->input('period', '7days');
        $minValue = auth()->user()->chart_first;
        $maxValue = auth()->user()->chart_last;
        $profitMargin = auth()->user()->chart_profit;
        
        $endDate = Carbon::today();
        $startDate = $endDate->copy();
        
        switch($period) {
            case '30days':
                $startDate->subDays(29);
                break;
            case '6months':
                $startDate->subMonths('6')->startOfMonth();
                break;
            default:
                $startDate->subDays(6);
                break;
        }
        
        $dummyData = $this->generateDummyData($startDate, $endDate, $minValue, $maxValue, $profitMargin);
        
        return response()->json($dummyData);
    }
    
    private function generateDummyData($startDate, $endDate, $minValue, $maxValue, $profitMargin)
    {
        $data = [];
        $currentDate = $startDate->copy();
        
        while($currentDate->lte($endDate)) {
            $total = random_int($minValue, $maxValue);
            
            $data[] = [
                'date' => $currentDate->format('Y-m-d'),
                'total' => $total,
                'profits' => $total * $profitMargin/100,
            ];
            
            $currentDate->addDay();
        }
        
        return $data;
    }
}