<?php

    namespace Modules\Lpj\Exports;

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

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

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

        public function collection()
        {
            $query = Permohonan::query();

            // Apply role-based filtering
            if (!Auth::user()->hasAnyRole(['administrator'])) {
                $query->where('branch_id', Auth::user()->branch_id);
            }


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

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

            // Apply branch filter if provided
            if ($this->request->has('branch_id') && !empty($this->request->branch_id)) {
                $query->where('branch_id', $this->request->branch_id);
            }

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

            // Default ordering
            $query->orderBy('nomor_registrasi', 'asc');

            return $query->get();
        }

        public function map($permohonan): array
        {
            return [
                $permohonan->id,
                $permohonan->nomor_registrasi,
                $permohonan->tanggal_permohonan,
                $permohonan->user ? $permohonan->user->name : '',
                $permohonan->branch ? $permohonan->branch->name : '',
                $permohonan->tujuanPenilaian ? $permohonan->tujuanPenilaian->name : '',
                $permohonan->jenisFasilitasKredit ? $permohonan->jenisFasilitasKredit->name : '',
                $permohonan->jenisPenilaian ? $permohonan->jenisPenilaian->name : '',
                $permohonan->status,
            ];
        }

        public function headings(): array
        {
            return [
                'ID',
                'Nomor Registrasi',
                'Tanggal Permohonan',
                'Pemohon',
                'Cabang',
                'Tujuan Penilaian',
                'Jenis Fasilitas Kredit',
                'Jenis Penilaian',
                'Status',
            ];
        }
    }
