<?php

    namespace Modules\Lpj\Exports;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    use Maatwebsite\Excel\Concerns\WithMapping;
    use Modules\Lpj\Models\PermohonanPembatalan;

    class LaporanPembatalanExport implements FromCollection, WithHeadings, WithMapping
    {
        protected $request;

        public function __construct(Request $request)
        {
            $this->request = $request;
        }

        public function collection()
        {
            $query = PermohonanPembatalan::where('status', 'batal');

            if (!Auth::user()->hasAnyRole(['administrator'])) {
                $query = $query->whereHas('permohonan', function ($q) {
                    $q->where('branch_id', Auth::user()->branch_id);
                });
            }

            // Apply search filter if provided
            if ($this->request->has('search') && !empty($this->request->get('search'))) {
                $search = $this->request->get('search');
                $query->where(function ($q) use ($search) {
                    $q->whereHas('permohonan', function ($subq) use ($search) {
                        $subq->where('nomor_registrasi', 'LIKE', '%' . $search . '%');
                        $subq->orWhereRelation('user', 'name', 'LIKE', '%' . $search . '%');
                        $subq->orWhereRelation('branch', 'name', 'LIKE', '%' . $search . '%');
                    });
                    $q->orWhere('alasan', 'LIKE', '%' . $search . '%');
                    $q->orWhere('status', 'LIKE', '%' . $search . '%');
                });
            }

            // Filter by date range if provided
            if ($this->request->has('start_date') || $this->request->has('end_date')) {
                $query->whereBetween('created_at', [
                    $this->request->get('start_date') ?? '1900-01-01',
                    $this->request->get('end_date') ?? now()->toDateString()
                ]);
            }

            // Filter by status if provided
            if ($this->request->has('status') && !empty($this->request->get('status'))) {
                $query->where('status', $this->request->get('status'));
            }

            // Filter by branch if provided
            if ($this->request->has('branch_id') && !empty($this->request->get('branch_id'))) {
                $query->whereHas('permohonan', function ($q) {
                    $q->where('branch_id', $this->request->get('branch_id'));
                });
            }

            return $query->orderBy('created_at', 'desc')
                         ->get();
        }

        public function headings(): array
        {
            return [
                'No. Registrasi',
                'Tanggal Permohonan',
                'Tanggal Pembatalan',
                'Cabang',
                'Pemohon',
                'Debitur',
                'Alasan Pembatalan',
                'Status',
                'Diajukan Oleh',
                'Disetujui Oleh',
                'Tanggal Disetujui'
            ];
        }

        public function map($pembatalan): array
        {
            return [
                $pembatalan->permohonan->nomor_registrasi ?? '-',
                $pembatalan->permohonan->tanggal_permohonan ? date('d-m-Y', strtotime($pembatalan->permohonan->tanggal_permohonan)) : '-',
                date('d-m-Y', strtotime($pembatalan->created_at)),
                $pembatalan->permohonan->branch->name ?? '-',
                $pembatalan->permohonan->user->name ?? '-',
                $pembatalan->permohonan->debiture->name ?? '-',
                $pembatalan->alasan_pembatalan,
                $pembatalan->status,
                $pembatalan->user->name ?? '-',
                $pembatalan->authorized_by ? $pembatalan->authorizedUser->name : '-',
                $pembatalan->authorized_at ? formatTanggalIndonesia(strtotime($pembatalan->authorized_at),1) : '-'
            ];
        }
    }
