﻿<?php

namespace App\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class JobExperienceResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'job_title' => $this->job_title,
            'company_name' => $this->company_name,
            'level' => $this->level,
            'work_status' => $this->work_status,
            'work_status_display' => $this->getWorkStatusDisplay(),
            'still_employed' => $this->still_employed,
            'employment_date' => $this->employment_date,
            'end_date' => $this->end_date,
            'duration' => $this->duration,
            'number_of_years_as_employee' => $this->number_of_years_as_employee,
            'years_of_experience' => $this->years_of_experience,
            'years_in_level' => $this->years_in_level,
            'location' => $this->location,
            'city' => $this->city,
            'state' => $this->state,
            'country' => $this->country,
            'work_arrangement' => $this->work_arrangement,
            'work_arrangement_display' => $this->getWorkArrangementDisplay(),
            'employment_type' => $this->employment_type,
            'employment_type_display' => $this->getEmploymentTypeDisplay(),
            'annual_base_salary' => $this->annual_base_salary,
            'formatted_salary' => $this->formatted_salary,
            'bonus_commission' => $this->bonus_commission,
            'overtime_pay' => $this->overtime_pay,
            'equity' => $this->equity,
            'currency' => $this->currency,
            'total_compensation' => $this->total_compensation,
            'benefits' => $this->benefits,
            'benefits_array' => $this->benefits ? explode(', ', $this->benefits) : [],
            'is_active' => $this->is_active,
            'is_published' => $this->is_published,
            'published_at' => $this->published_at,
            'published_at_formatted' => $this->published_at?->format('M j, Y'),
            'job_description' => $this->job_description,
            'industry' => $this->industry,
            'company_size' => $this->company_size,
            'average_rating' => $this->average_rating,
            'ratings_count' => $this->ratings_count,
            'created_at' => $this->created_at,
            'created_at_formatted' => $this->created_at->format('M j, Y'),
            'updated_at' => $this->updated_at,
            // 'user' => new UserResource($this->whenLoaded('user')),
            //'ratings' => PayRatingResource::collection($this->whenLoaded('ratings')),
        ];
    }

    // Helper methods for display values
    private function getWorkStatusDisplay(): string
    {
        return match($this->work_status) {
            'new_offer' => 'New Offer',
            'current_employee' => 'Current Employee',
            'former_employee' => 'Former Employee',
            default => ucfirst(str_replace('_', ' ', $this->work_status))
        };
    }

    private function getWorkArrangementDisplay(): string
    {
        return match($this->work_arrangement) {
            'onsite' => 'On-site',
            'remote' => 'Remote',
            'hybrid' => 'Hybrid',
            default => ucfirst($this->work_arrangement)
        };
    }

    private function getEmploymentTypeDisplay(): string
    {
        return match($this->employment_type) {
            'full_time' => 'Full Time',
            'part_time' => 'Part Time',
            'contract' => 'Contract',
            'freelance' => 'Freelance',
            'internship' => 'Internship',
            default => ucfirst(str_replace('_', ' ', $this->employment_type))
        };
    }