<?php

namespace App\Http\Controllers\Api;

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

class ProjectController extends BaseApiController
{
    protected string $model = Project::class;
    protected array $searchable = ['name', 'location', 'category', 'status', 'PRJID'];
    protected array $filters = ['status' => 'status', 'category' => 'category'];

    protected function rules(Request $request, ?Model $model = null): array
    {
        $required = $model ? 'sometimes' : 'required';
        return [
            'PRJID' => 'nullable|string',
            'name' => "$required|string|max:255",
            'location' => 'nullable|string',
            'category' => 'nullable|string',
            'status' => 'nullable|string',
            'budget' => 'nullable|string',
            'timeline' => 'nullable|string',
            'contractor' => 'nullable|string',
            'teamSize' => 'nullable|string',
            'description' => 'nullable|string',
            'progress' => 'nullable|integer|min:0|max:100',
        ];
    }

    protected function transform(Request $request, array $data): array
    {
        if (empty($data['PRJID']) && !$request->route('id')) {
            $data['PRJID'] = 'PRJ-' . str_pad((string) (Project::withTrashed()->count() + 1), 4, '0', STR_PAD_LEFT);
        }
        return $data;
    }
}
