<?php

namespace App\Repositories;

use App\Models\Enquiry;
use App\Repositories\Interfaces\EnqueryInterface;
use Illuminate\Http\Request;

class EnqueryRepository implements EnqueryInterface
{
    public function store(array $data)
    {
        return Enquiry::create($data);
    }

    public function list(Request $request)
    {
        $query = Enquiry::where('is_delete', 0);

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('email', 'like', "%{$search}%")
                  ->orWhere('phone', 'like', "%{$search}%");
            });
        }

        if ($request->filled('from_date')) {
            $query->whereDate('created_at', '>=', $request->from_date);
        }
        if ($request->filled('to_date')) {
            $query->whereDate('created_at', '<=', $request->to_date);
        }

        return $query->orderBy('id', 'desc')->paginate(20);
    }

    public function find($id)
    {
        return Enquiry::where('is_delete', 0)->find($id);
    }

    public function delete($id)
    {
        $enquiry = $this->find($id);
        if ($enquiry) {
            $enquiry->update(['is_delete' => 1]);
            return true;
        }
        return false;
    }
}
