<?php

namespace App\Http\Requests\Profile;

use App\Models\StoredFile;
use App\Services\File\StoredFileService;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class UpdateProfileRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $userId = $this->user()->id;

        return [
            'name' => ['required', 'string', 'max:255'],
            'username' => [
                'nullable',
                'string',
                'max:255',
                Rule::unique('users', 'username')->ignore($userId),
            ],
            'email' => [
                'required',
                'email',
                'max:255',
                Rule::unique('users', 'email')->ignore($userId),
            ],
            'whatsapp_number' => ['nullable', 'string', 'max:20'],
            'profile_photo_file_id' => ['nullable', 'integer', 'exists:stored_files,id'],
            'current_password' => ['nullable', 'required_with:password', 'string'],
            'password' => ['nullable', 'string', 'min:8', 'confirmed'],

            'user_profile' => ['nullable', 'array'],
            'user_profile.nik' => ['nullable', 'string', 'max:30'],
            'user_profile.address' => ['nullable', 'string'],
            'user_profile.provinsi_id' => ['nullable', 'integer', 'exists:wilayah_provinsi,id'],
            'user_profile.kabupaten_id' => [
                'nullable',
                'integer',
                Rule::exists('wilayah_kabupaten', 'id')->where(
                    'provinsi_id',
                    $this->input('user_profile.provinsi_id')
                ),
            ],
            'user_profile.kecamatan_id' => [
                'nullable',
                'integer',
                Rule::exists('wilayah_kecamatan', 'id')->where(
                    'kabupaten_id',
                    $this->input('user_profile.kabupaten_id')
                ),
            ],
            'user_profile.desa_id' => [
                'nullable',
                'integer',
                Rule::exists('wilayah_desa', 'id')->where(
                    'kecamatan_id',
                    $this->input('user_profile.kecamatan_id')
                ),
            ],
        ];
    }

    public function after(): array
    {
        return [
            function (Validator $validator) {
                if ($this->filled('password')
                    && ! Hash::check((string) $this->input('current_password'), $this->user()->password)) {
                    $validator->errors()->add(
                        'current_password',
                        'Password saat ini tidak sesuai.'
                    );
                }

                if ($this->filled('profile_photo_file_id')) {
                    $file = StoredFile::find($this->integer('profile_photo_file_id'));

                    if ($file) {
                        app(StoredFileService::class)->validateProfilePhoto($file, $this->user());
                    }
                }
            },
        ];
    }
}
