<?php

namespace App\Http\Controllers\Api\V1;

use App\Domain\Identity\Models\Branch;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class BranchController extends Controller
{
    public function index(): JsonResponse
    {
        $branches = Branch::where('is_active', true)
            ->select([
                'id', 'name', 'name_ar', 'address', 'city', 'governorate',
                'phone', 'email', 'latitude', 'longitude', 'operating_hours', 'is_main',
            ])
            ->orderByDesc('is_main')
            ->orderBy('name_ar')
            ->get();

        return response()->json([
            'data' => $branches->map(fn ($b) => [
                'id' => $b->id,
                'name' => $b->name,
                'name_ar' => $b->name_ar,
                'address' => $b->address,
                'city' => $b->city,
                'governorate' => $b->governorate,
                'phone' => $b->phone,
                'email' => $b->email,
                'latitude' => $b->latitude ? (float) $b->latitude : null,
                'longitude' => $b->longitude ? (float) $b->longitude : null,
                'operating_hours' => $b->operating_hours,
                'is_main' => $b->is_main,
            ]),
        ]);
    }
}
