<?php

namespace App\Charts;

use ArielMejiaDev\LarapexCharts\LarapexChart;
use App\Models\Payment;

class PaymentDonutChart
{
    protected $chart;

    public function __construct(LarapexChart $chart)
    {
        $this->chart = $chart;
    }

    public function build(): \ArielMejiaDev\LarapexCharts\DonutChart
    {
        $total = Payment::all()->count();
        $success = Payment::where('status','success')->count();
        $pending = Payment::where('status','pending')->count();
        $failed = Payment::where('status','failed')->count();

         
 
         // Format percentages to two decimal places
         $successPercentage = $total <= 0 ? 0 : round($success * 100 / $total, 2);
        $failedPercentage = $total <= 0 ? 0 : round($failed * 100 / $total, 2);
        $pendingPercentage = $total <= 0 ? 0 : round($pending * 100 / $total, 2);


        return $this->chart->donutChart()
            ->setTitle('Contribution Status in %')
            // ->setSubtitle('Season 2021.')
            ->addData([
                $successPercentage,
                $failedPercentage,
                $pendingPercentage
            ])
            ->setColors([ '#38ef7d','#7c074d','#a86008',])
            ->setLabels(['SUCCESS', 'FAILED', 'PENDING']);
    }
}
