<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;

class VoucherPromoPoint extends Model
{
    public $timestamps = true;
    use SoftDeletes;
    protected $table = 'voucher_promo_point';
    protected $perPage = 20;

    protected $fillable = ['nama_voucher', 'promo_point_id', 'kuota', 'kriteria_coin', 'deskripsi', 'status', 'tgl_active', 'tgl_inactive', 'image', 'external_code', 'alokasi', 'created_who', 'updated_who', 'redeem'];

    public function promoPoint()
    {
        return $this->belongsTo(PromoPoint::class, 'promo_point_id');
    }

    public static function getCouponList()
    {
        $today = Carbon::now();
        $vouchers = VoucherPromoPoint::whereDate('tgl_active', '<=', $today)
            ->whereDate('tgl_inactive', '>=', $today)
            ->where('status', 'Aktif')
            ->select('id', 'image', 'external_code', 'nama_voucher', 'alokasi', 'kuota')
            ->get();

        $voucherList = [];

        foreach ($vouchers as $voucher) {
            $voucherList[] = [
                'id' => $voucher->id,
                'imageUrl' => asset('images/vouchergb/' . $voucher->image),
                'externalCode' => $voucher->external_code,
                'subject' => $voucher->nama_voucher,
                'pointType' => 'GB',
                'allocation' => $voucher->alokasi,
                'remainingQuota' => $voucher->kuota,
                'redeem' => $voucher->redeem
            ];
        }

        return $voucherList;
    }
}
