<?php


namespace App\Http\Controllers;

use Carbon;
use Request;
use App\Models\PaymentReminder;
use App\Models\ShoppingPayment;
use App\Services\PaymentReminderService;

class PaymentReminderController extends Controller
{

    private $filter_user_status;
    private $paymentReminderService;

    public function __construct(PaymentReminderService $paymentReminderService)
    {
        $this->middleware('auth');
        $this->paymentReminderService = $paymentReminderService;
    }

    public function index()
    {
        // Hole die detaillierten Daten für die Tabellen-Ansicht
        $detailedData = $this->paymentReminderService->getDetailedPaymentsData();
       // $summaryData = $this->paymentReminderService->getAllOpenPayments();
        
        // Statistiken für die Übersicht
        $totalPayments = collect($detailedData)->count();
        $totalAmount = collect($detailedData)->sum('amount')/100;
        $clearingTypes = collect($detailedData)->groupBy('clearingtype')->map->count();
        
        $data = [
            'reminders' => PaymentReminder::all(),
            'detailedData' => $detailedData,
            //'summaryData' => $summaryData,
            'totalPayments' => $totalPayments,
            'totalAmount' => $totalAmount,
            'clearingTypes' => $clearingTypes,
        ];
        return view('admin.payment.reminder.index', $data);
    }

    public function create()
    {
        $reminder = new PaymentReminder();
        $reminder->active = true;
        $data = [
            'reminder' => $reminder,
        ];
        return view('admin.payment.reminder.edit', $data);
    }

    public function edit($id)
    {
        $data = [
            'reminder' => PaymentReminder::find($id),
        ];
        return view('admin.payment.reminder.edit', $data);
    }

    public function store()
    {
        $data = Request::all();
        $data['active'] = isset($data['active']) ? true : false;
        $data['action'] = isset($data['action']) ? $data['action'] : NULL;

        if ($data['id'] === 'new') {
            PaymentReminder::create($data);
        } else {
            $reminder = PaymentReminder::find($data['id']);
            $reminder->update($data);
        }
        return redirect()->route('admin_payments_reminder')->with('success', 'Erinnerung gespeichert');
    }

    public function action($action, $id)
    {
        
        $payment = ShoppingPayment::find($id);
        if($action == 'send_reminder'){
            $bool = $this->paymentReminderService->sendReminder($payment);
            if($bool){
                \Session()->flash('alert-success', "Zahlungserinnerung gesendet");
            }else{
                \Session()->flash('alert-error', "Keine Zahlungserinnerung gesendet");
            }
        }
        if($action == 'no_payment'){
            $this->paymentReminderService->setNoNPayment($payment);
            \Session()->flash('alert-success', "Zahlung als nicht bezahlt markiert");
        }
        return redirect()->route('admin_payments_reminder');
    }

    public function delete($id)
    {
        $reminder = PaymentReminder::find($id);
        $reminder->delete();
        return redirect()->route('admin_payments_reminder');
    }

    public function logs()
    {
        // Hole die Log-Statistiken für verschiedene Zeiträume
        $stats7Days = $this->paymentReminderService->getLogStatistics(7);
        $stats30Days = $this->paymentReminderService->getLogStatistics(30);
        $stats90Days = $this->paymentReminderService->getLogStatistics(90);
        
        // Hole die neuesten Logs
        $recentLogs = $this->paymentReminderService->getPaymentReminderLogs(50);
        
        // Filter-Parameter
        $orderId = Request::get('order_id');
        $action = Request::get('action');
        $startDate = Request::get('start_date');
        $endDate = Request::get('end_date');
        
        // Gefilterte Logs
        $filteredLogs = null;
        if ($orderId || $action || $startDate || $endDate) {
            if ($startDate && $endDate) {
                $filteredLogs = $this->paymentReminderService->getLogsForDateRange($startDate, $endDate);
            } elseif ($orderId) {
                $filteredLogs = $this->paymentReminderService->getLogsForPayment($orderId);
            } elseif ($action) {
                $filteredLogs = $this->paymentReminderService->getPaymentReminderLogs(100, null, $action);
            }
        }
        
        $data = [
            'stats7Days' => $stats7Days,
            'stats30Days' => $stats30Days,
            'stats90Days' => $stats90Days,
            'recentLogs' => $recentLogs,
            'filteredLogs' => $filteredLogs,
            'orderId' => $orderId,
            'action' => $action,
            'startDate' => $startDate,
            'endDate' => $endDate,
        ];
        
        return view('admin.payment.reminder.logs', $data);
    }
}