<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Office;
use Illuminate\Http\Request;

class OfficeController extends Controller
{
    /**
     * Get list of offices
     */
    public function index(Request $request)
    {
        $user = $request->user();
        $query = Office::query();

        // Filter by user's company by default (unless explicitly specified)
        if ($request->has('company_id')) {
            $query->where('company_id', $request->company_id);
        } elseif ($user && $user->company_id) {
            $query->where('company_id', $user->company_id);
        }

        // Only get active offices
        $query->where('is_active', true);

        // Filter by type
        if ($request->has('type')) {
            $query->where('type', $request->type);
        }

        // Search by name
        if ($request->has('search')) {
            $query->where('name', 'like', '%' . $request->search . '%');
        }

        $offices = $query->select([
            'id',
            'name',
            'type',
            'address',
            'city',
            'province',
            'latitude',
            'longitude',
            'radius'
        ])->get();

        return response()->json([
            'success' => true,
            'data' => $offices
        ]);
    }

    /**
     * Get single office
     */
    public function show(Office $office)
    {
        return response()->json([
            'success' => true,
            'data' => $office
        ]);
    }
}
