<?php

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\ReporterApplication;

return new class extends Component
{
    use WithFileUploads;

    // Personal Details
    public $full_name = '';
    public $father_husband_name = '';
    public $dob = '';
    public $gender = '';
    public $address = '';
    public $mobile_number = '';
    public $whatsapp_number = '';
    public $email = '';

    // Education
    public $highest_qualification = '';
    public $school_college_name = '';

    // Reporting Details
    public $applying_for = [];
    public $area = '';

    // Experience & Skills
    public $has_experience = false;
    public $experience_details = '';
    public $skills = [];

    // Equipment
    public $has_smartphone = false;
    public $has_microphone = false;
    public $has_internet = false;

    // Availability
    public $availability_daily = false;

    // Uploads
    public $photo;
    public $id_proof;

    public $submitted = false;

    public function render()
    {
        return $this->view()->layout('layouts::frontend');
    }

    public function submit()
    {
        $this->validate([
            'full_name'           => 'required|string|max:255',
            'mobile_number'       => 'required|string|max:20',
            'gender'              => 'required',
            'applying_for'        => 'required|array|min:1',
            'area'                => 'required|string|max:255',
            'photo'               => 'nullable|image|max:2048',
            'id_proof'            => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:4096',
        ]);

        $photoPath   = $this->photo   ? $this->photo->store('applications/photos', 'public')    : null;
        $idProofPath = $this->id_proof ? $this->id_proof->store('applications/id_proofs', 'public') : null;

        ReporterApplication::create([
            'full_name'           => $this->full_name,
            'father_husband_name' => $this->father_husband_name,
            'dob'                 => $this->dob ?: null,
            'gender'              => $this->gender,
            'address'             => $this->address,
            'mobile_number'       => $this->mobile_number,
            'whatsapp_number'     => $this->whatsapp_number,
            'email'               => $this->email,
            'highest_qualification' => $this->highest_qualification,
            'school_college_name' => $this->school_college_name,
            'applying_for'        => $this->applying_for,
            'area'                => $this->area,
            'has_experience'      => $this->has_experience,
            'experience_details'  => $this->experience_details,
            'skills'              => $this->skills,
            'has_smartphone'      => $this->has_smartphone,
            'has_microphone'      => $this->has_microphone,
            'has_internet'        => $this->has_internet,
            'availability_daily'  => $this->availability_daily,
            'photo_path'          => $photoPath,
            'id_proof_path'       => $idProofPath,
            'status'              => 'pending',
        ]);

        $this->submitted = true;
    }

    protected function view($data = [])
    {
        return app('view')->file('D:\02-my freelance\00-DR_Padhi\DR_PADHI\storage\framework/views/livewire/views/4e9e7a74.blade.php', $data);
    }
};
