<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserProfile extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'role_specific_data',
        'company_name',
        'address',
        'city',
        'state',
        'country',
        'postal_code',
        'emergency_contact_name',
        'emergency_contact_phone',
        'bio',
        'website',
        'linkedin',
        'twitter',
        'facebook',
        'instagram',
        'certification',
        'specialization',
        'experience_years',
        'fitness_level',
        'goals',
        'medical_conditions',
        'trainer_preference',
        'expertise_areas',
        'mentoring_experience',
        'availability',
        'learning_goals',
        'experience_level',
        'mentor_preference',
        'partnership_type',
        'contact_person',
        'contribution_type',
        'portfolio_url',
        'skills',
        'marketing_focus',
        'target_audience',
        'campaign_experience',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'role_specific_data' => 'array',
    ];

    /**
     * Get the user that owns the profile.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get a role-specific field value.
     *
     * @param string $field
     * @param mixed $default
     * @return mixed
     */
    public function getRoleField(string $field, mixed $default = null): mixed
    {
        // First check if it's a direct attribute
        if (isset($this->attributes[$field])) {
            return $this->attributes[$field];
        }

        // Then check in role_specific_data
        if ($this->role_specific_data && isset($this->role_specific_data[$field])) {
            return $this->role_specific_data[$field];
        }

        return $default;
    }

    /**
     * Set a role-specific field value.
     *
     * @param string $field
     * @param mixed $value
     * @return void
     */
    public function setRoleField(string $field, mixed $value): void
    {
        if (in_array($field, $this->fillable)) {
            $this->attributes[$field] = $value;
        } else {
            $data = $this->role_specific_data ?? [];
            $data[$field] = $value;
            $this->role_specific_data = $data;
        }
    }

    /**
     * Get fields specific to a role.
     *
     * @param string $roleName
     * @return array
     */
    public static function getRoleSpecificFields(string $roleName): array
    {
        $fields = [
            'Members' => ['phone', 'address', 'emergency_contact_name', 'emergency_contact_phone'],
            'Trainers' => ['certification', 'specialization', 'experience_years', 'bio'],
            'Trainees' => ['fitness_level', 'goals', 'medical_conditions', 'trainer_preference'],
            'Mentor' => ['expertise_areas', 'mentoring_experience', 'availability', 'bio'],
            'Mentee' => ['learning_goals', 'experience_level', 'mentor_preference', 'bio'],
            'Partners' => ['company_name', 'partnership_type', 'contact_person', 'address'],
            'Contributor' => ['contribution_type', 'portfolio_url', 'skills', 'bio'],
            'Health Marketor' => ['marketing_focus', 'target_audience', 'campaign_experience', 'bio'],
        ];

        return $fields[$roleName] ?? [];
    }

    /**
     * Get full address.
     *
     * @return string
     */
    public function getFullAddressAttribute(): string
    {
        $parts = array_filter([
            $this->address,
            $this->city,
            $this->state,
            $this->postal_code,
            $this->country,
        ]);

        return implode(', ', $parts);
    }

    /**
     * Get social links as array.
     *
     * @return array
     */
    public function getSocialLinksAttribute(): array
    {
        $links = [];

        if ($this->website) {
            $links['website'] = $this->website;
        }
        if ($this->linkedin) {
            $links['linkedin'] = $this->linkedin;
        }
        if ($this->twitter) {
            $links['twitter'] = $this->twitter;
        }
        if ($this->facebook) {
            $links['facebook'] = $this->facebook;
        }
        if ($this->instagram) {
            $links['instagram'] = $this->instagram;
        }

        return $links;
    }
}
