<?php
namespace App\Models;

use CodeIgniter\Model;

class AdminReportModel extends Model
{
    protected $table = 'orders';
    protected $primaryKey = 'id';
    protected $allowedFields = [
        'apartment_id', 'customer_name', 'email', 'phone', 
        'address', 'payment_method', 'delivery_charge', 
        'total_amount', 'status', 'created_at'
    ];

    // Fetch all orders with apartment details
    public function getAllOrdersWithApartment()
    {
        return $this->select('orders.*, apartmentdetails.apartName, apartmentdetails.apartAddress')
                    ->join('apartmentdetails', 'apartmentdetails.id = orders.apartment_id', 'left')
                    ->orderBy('orders.created_at', 'DESC')
                    ->findAll();
    }

    // Fetch orders by status
    public function getOrdersByStatus($status)
    {
        return $this->select('orders.*, apartmentdetails.apartName, apartmentdetails.apartAddress')
                    ->join('apartmentdetails', 'apartmentdetails.id = orders.apartment_id', 'left')
                    ->where('orders.status', $status)
                    ->orderBy('orders.created_at', 'DESC')
                    ->findAll();
    }

    // Fetch orders by date range
    public function getOrdersByDateRange($from, $to)
    {
        return $this->select('orders.*, apartmentdetails.apartName, apartmentdetails.apartAddress')
                    ->join('apartmentdetails', 'apartmentdetails.id = orders.apartment_id', 'left')
                    ->where('orders.created_at >=', $from.' 00:00:00')
                    ->where('orders.created_at <=', $to.' 23:59:59')
                    ->orderBy('orders.created_at', 'DESC')
                    ->findAll();
    }
}
