<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\HoseaResource;
use App\Models\Article;
use App\Models\Hosea;
use DB;
use Illuminate\Http\Request;

class HoseaController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $data = Hosea::where(function ($query) use ($request) {
            if ($request->city)
                $query->where('city', '=', $request->city);
        })->latest()->get();

        return HoseaResource::collection($data);
    }

    public function show($slug)
    {
        try {
            $hosea = Hosea::where('slug', $slug)->get();
            if (!$hosea) {
                abort(404);
            }

            return HoseaResource::collection($hosea);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}