<?php

namespace App\Http\Requests\Campaign;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreCampaignRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    /**
     * @return array<string, ValidationRule|array<int, mixed>|string>
     */
    public function rules(): array
    {
        return [
            'judul' => ['required', 'string', 'max:255'],
            'url' => ['required', 'string', 'max:1000'],
            'jam_tayang_mulai' => ['required', 'date_format:H:i'],
            'jam_tayang_selesai' => ['required', 'date_format:H:i', 'after:jam_tayang_mulai'],
            'credit' => ['nullable', 'integer', 'min:0'],
            'budget_harian' => ['required', 'integer', 'min:1'],
            // Kategori wajib hanya admin; member tidak pilih kategori (BR-12), admin tentukan nanti.
            'kategori_ids' => ['nullable', 'array', Rule::requiredIf(fn () => $this->user()?->isAdmin() ?? false)],
            'kategori_ids.*' => [Rule::exists('kategoris', 'id')],
            // Jenis iklan + platform + negara.
            'ad_type' => ['nullable', 'string', 'in:affiliate,link_website,backlink,press_release'],
            'ad_platform_id' => ['nullable', Rule::exists('ad_platforms', 'id')],
            'target_country' => ['nullable', 'string', 'max:5'],
            // Admin-only fields:
            'user_id' => ['nullable', Rule::exists('users', 'id')],
        ];
    }
}
