<?php

namespace App\Exports;

use App\Models\Order;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Carbon\Carbon;

class FilteredDataOrderExport implements FromCollection, WithHeadings, WithMapping
{
    protected $filters;

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

    public function collection()
    {
        $query = Order::with('user', 'service', 'service_provider')->select('orders.*');
        
        // Apply filters based on the provided inputs
        if (isset($this->filters['filter_status']) && $this->filters['filter_status'] !== '') {
            $query->where('status', $this->filters['filter_status']);
        }

        if (isset($this->filters['filter_user']) && $this->filters['filter_user'] !== '') {
            $query->whereHas('user', function($query){
                $query->where('username', 'like', "%".escape_input($this->filters['filter_user'])."%");
            });
        }

        if (isset($this->filters['filter_service']) && $this->filters['filter_service'] !== '') {
            $query->where('service_name', $this->filters['filter_service']);
        }

        if (isset($this->filters['filter_service_provider']) && $this->filters['filter_service_provider'] !== '') {
            $query->where('provider_id', $this->filters['filter_service_provider']);
        }

            // Apply filters based on the provided inputs
        if (!empty($this->filters['filter_start_date']) && !empty($this->filters['filter_end_date'])) {
            $start_date = month_format($this->filters['filter_start_date']);
            $end_date = month_format($this->filters['filter_end_date']);
            $query->whereRaw('DATE(created_at) BETWEEN "'.date('Y-m-d', strtotime($start_date)).'" AND "'.date('Y-m-d', strtotime($end_date)).'"');
        }

        if (isset($this->filters['filter_is_api']) && $this->filters['filter_is_api'] !== '') {
            $query->where('is_api', $this->filters['filter_is_api']);
        }

        // Add more filters as needed

        return $query->get(['id', 'provider_order_id', 'user_id', 'service_name', 'quantity', 'status', 'created_at', 'price']);
    }

    public function headings(): array
    {
        return [
            'ID',  // id & created_at
            'Tanggal',
            'PID',           // provider_order_id
            'Pengguna',      // user_id
            'Layanan',       // service_name
            'Jumlah',  // price
            'Harga',
            'Status',        // status
        ];
    }

   public function map($order): array
    {
        $createdAt = Carbon::parse($order->created_at)->format('Y-m-d');
        $serviceName = $order->service ? $order->service->name : ($order->service_name ?: 'Unknown Service');
    
        return [
            $order->id,
            $createdAt, // Format to show only the date
            $order->provider_order_id,
            $order->user ? $order->user->username : 'Unknown User',
            $serviceName, // Pre-computed service name
            $order->quantity,
            $order->price,
            $order->status,
        ];
    }
}
