<?php

namespace App\Models;

use App\Casts\AmountCast;
use App\Enumerations\EatingHabit;
use App\Enumerations\EfzStatus;
use App\Enumerations\FirstAidPermission;
use App\Enumerations\ParticipationType;
use App\Enumerations\SwimmingPermission;
use App\Scopes\InstancedModel;
use Illuminate\Database\Eloquent\Builder;


class EventParticipant extends InstancedModel
{
    protected $table = 'event_participants';

    protected $fillable = [
        'tenant',
        'event_id',
        'user_id',
        'identifier',

        'firstname',
        'lastname',
        'nickname',

        'participation_type',
        'local_group',
        'birthday',

        'address_1',
        'address_2',
        'postcode',
        'city',

        'email_1',
        'phone_1',
        'email_2',
        'phone_2',
        'contact_person',

        'allergies',
        'intolerances',
        'medications',
        'tetanus_vaccination',
        'eating_habit',
        'swimming_permission',
        'first_aid_permission',

        'foto_socialmedia',
        'foto_print',
        'foto_webseite',
        'foto_partner',
        'foto_intern',

        'arrival_date',
        'departure_date',
        'arrival_eating',
        'departure_eating',

        'notes',
        'amount',
        'amount_paid',
        'payment_purpose',
        'efz_status',
        'unregistered_at',
    ];

    protected $casts = [
        'birthday' => 'date',
        'tetanus_vaccination' => 'date',
        'arrival_date' => 'date',
        'departure_date' => 'date',
        'unregistered_at' => 'date',

        'foto_socialmedia' => 'boolean',
        'foto_print' => 'boolean',
        'foto_webseite' => 'boolean',
        'foto_partner' => 'boolean',
        'foto_intern' => 'boolean',

        'amount' => AmountCast::class,
        'amount_paid' => AmountCast::class,
    ];

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    */

    public function event()
    {
        return $this->belongsTo(Event::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function tenantRelation()
    {
        return $this->belongsTo(Tenant::class, 'tenant', 'slug');
    }

    public function localGroup()
    {
        return $this->belongsTo(Tenant::class, 'local_group', 'slug');
    }

    public function participationType()
    {
        return $this->belongsTo(ParticipationType::class, 'participation_type', 'slug');
    }

    public function swimmingPermission()
    {
        return $this->belongsTo(SwimmingPermission::class, 'swimming_permission', 'slug');
    }

    public function eatingHabit()
    {
        return $this->belongsTo(EatingHabit::class, 'eating_habits', 'slug');
    }

    public function firstAidPermission()
    {
        return $this->belongsTo(FirstAidPermission::class, 'first_aid_permission', 'slug');
    }

    public function efzStatus()
    {
        return $this->belongsTo(EfzStatus::class, 'efz_status', 'slug');
    }

    public function getOfficialName() : string {
        return sprintf('%1$s %2$s', $this->firstname, $this->lastname);
    }

    public function getFullName() : string {
        if ($this->nickname === null) {
            return sprintf('%1$s %2$s', $this->firstname, $this->lastname)
                |>trim(...);
        }


        return sprintf('%1$1s%2$s(%3$s %4$s)',
                $this->nickname,
                '<br />',
                $this->firstname,
                $this->lastname,
            )
                |>trim(...);
    }

    public function getNicename() : string {
        return $this->nickname ?? $this->firstname;
    }

}
