<?php

namespace App\Exports;

use App\Models\User;
use App\Models\Wallet;
use App\Models\WithdrawRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class WithdrawRequestExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles, WithEvents
{
    protected $request;
    protected $counter;
    protected $status;


    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->counter = 1;
        $this->status = $request->input('status');
    }

    public function collection()
    {
        $authUser = auth()->user();
        $userIds = User::query()->pluck('id')->toArray();
        $query = WithdrawRequest::whereIn('user_id', $userIds)->with(['user', 'user.userBankAccount'])->where('status', 'requested');

        if ($authUser->user_type !== 'admin') {
            $query->where('user_id', $authUser->id);
        }
        $withdraws = $query->get();

        $data = $withdraws->map(function ($row) {
            $wallet = Wallet::where('user_id', $row->user_id)->first();
            $totalAmount = $wallet ? getPriceFormat($wallet->total_amount) : 0;
            $bankDetails = optional($row->user->userBankAccount);

            return [
                'id' => $row->id,
                'name' => optional($row->user)->name,
                'amount' => getPriceFormat($row->amount),
                'created_at' => dateAgoFormate($row->created_at),
                'bank_name' => $bankDetails->bank_name ?? '-',
                'bank_account_holder_name' => $bankDetails->account_holder_name ?? '-',
                'bank_account_number' => $bankDetails->account_number ?? '-',
                'bank_ifsc_code' => $bankDetails->bank_code ?? '-',
                'bank_address' => $bankDetails->bank_address ?? '-',
                'routing_number' => $bankDetails->routing_number ?? '-',
                'bank_iban' => $bankDetails->bank_iban ?? '-',
                'bank_swift' => $bankDetails->bank_swift ?? '-',
            ];
        })->toArray();

        return collect($data);
    }

    public function map($row): array
    {
        return [
            $row['id'] ?? '-',
            $row['name'] ?? '-',
            $row['amount'] ?? '-',
            $row['created_at'] ?? '-',
            $row['bank_name'] ?? '-',
            $row['bank_account_holder_name'] ?? '-',
            $row['bank_account_number'] ?? '-',
            $row['bank_ifsc_code'] ?? '-',
            $row['bank_address'] ?? '-',
            $row['routing_number'] ?? '-',
            $row['bank_iban'] ?? '-',
            $row['bank_swift'] ?? '-',
        ];
    }

    public function headings($exportType = 'excel'): array
    {
        $headings = [
            [ __('message.pending_withdraw_request')],
            [''],
            [
                __('message.id'),
                __('message.name'),
                __('message.withdraw_amount'),
                __('message.request_at'),
                __('message.bank_name'),
                __('message.bank_account_holder_name'),
                __('message.bank_account_number'),
                __('message.bank_ifsc_code'),
                __('message.bank_address'),
                __('message.routing_number'),
                __('message.bank_iban'),
                __('message.bank_swift'),
            ],
        ];

        return $headings;
    }

    public function styles(Worksheet $sheet)
    {
        $sheet->mergeCells('A1:M1')->getStyle('A1:M1')->getFont()->setBold(true)->setSize(13);

        $sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('F3:M3')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);


            $sheet->getStyle('A3:M3')->applyFromArray([
                'font' => [
                    'bold' => true,
                ],
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                    'vertical' => Alignment::VERTICAL_CENTER,
                ],
            ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $sheet = $event->sheet->getDelegate();
                $highestRow = $sheet->getHighestRow();
                $sheet->getStyle('A:M')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
            },
        ];
    }
}
