<?php

namespace App\Http\Controllers\Api;

use App\Models\ScheduleEquipment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class ScheduleEquipmentController extends BaseApiController
{
    protected string $model = ScheduleEquipment::class;
    protected array $with = ['equipment', 'project'];
    protected array $searchable = ['operator', 'notes'];
    protected array $filters = ['status' => 'status', 'equipment' => 'equipment_id', 'project' => 'project_id'];

    protected function rules(Request $request, ?Model $model = null): array
    {
        return [
            'equipment' => 'nullable|string|exists:equipment,id',
            'project' => 'nullable|string|exists:projects,id',
            'startDate' => 'nullable|date',
            'endDate' => 'nullable|date',
            'operator' => 'nullable|string',
            'notes' => 'nullable|string',
            'status' => 'nullable|string',
        ];
    }

    protected function transform(Request $request, array $data): array
    {
        foreach (['equipment' => 'equipment_id', 'project' => 'project_id'] as $k => $col) {
            if (array_key_exists($k, $data)) { $data[$col] = $data[$k]; unset($data[$k]); }
        }
        return $data;
    }
}
