<?php

namespace App\Exports;

use App\Models\Coupon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class CouponExport implements FromCollection, WithHeadings, WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $coupons = Coupon::searchable(['code','user:username,id,system_id'])->dateFilter('published_at')->with('user');
        return $coupons->get();
    }

    public function map($user): array
    {
        return [
            $user->user->system_id,
            $user->user->username,
            $user->user->email,
            $user->user->mobile,
            $user->code,
            showDateTime($user->published_at),
            showDateTime($user->expires_at),
            $user->used_at,
        ];
    }
    public function headings(): array
    {
        return [
            'User Id',
            'UserName',
            'Email Id',
            'Mobile',
            'Code',
            'Published At',
            'Expires At',
            'Used At',
        ];
    }
}
