<?php

namespace App\Repositories\Eloquent;

use App\Models\Payment;
use Illuminate\Database\Eloquent\Model;

class PaymentRepository extends BaseRepository
{
    protected function resolveModel(): Model
    {
        return new Payment();
    }

    public function getForOrder(int $orderId)
    {
        return $this->model->where('order_id', $orderId)
            ->with('user')
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getByDateRange(int $branchId, $startDate, $endDate)
    {
        return $this->model->whereHas('order', function ($query) use ($branchId) {
            $query->where('branch_id', $branchId);
        })
            ->whereBetween('created_at', [$startDate, $endDate])
            ->completed()
            ->with(['order', 'user'])
            ->get();
    }

    public function getSummaryByMethod(int $branchId, $startDate, $endDate)
    {
        return $this->model->whereHas('order', function ($query) use ($branchId) {
            $query->where('branch_id', $branchId);
        })
            ->whereBetween('created_at', [$startDate, $endDate])
            ->completed()
            ->selectRaw('method, COUNT(*) as count, SUM(amount) as total')
            ->groupBy('method')
            ->get();
    }
}
