<?php

namespace Ignite\Core\Entities;

use Illuminate\Database\Eloquent\Builder;
use Ignite\Packages\Presenter\Contracts\Presenter;
use Ignite\Packages\Presenter\Traits\Presentable;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Participant
 *
 * @property int user_id
 * @property string title
 * @property string email
 * @property string first
 * @property string last
 * @property string type
 * @property string employee_id
 * @property string phone1
 * @property string phone2
 * @property string phone3
 * @property string company
 * @property string department_1
 * @property string department_2
 * @property string address_1
 * @property string address_2
 * @property string city
 * @property string state
 * @property string postal
 * @property string country
 * @property string work_address_1
 * @property string work_address_2
 * @property string work_city
 * @property string work_state
 * @property string work_postal
 * @property string work_country
 * @property string alternate_email
 * @property string region
 * @property string dob
 * @property string ssn
 * @property string agree_confirm_1
 * @property string agree_confirm_2
 * @property string status
 * @property string archived
 * @property string idc
 * @property int internal
 * @property string internal_notes
 * @property string approved_at
 * @property string deleted_at
 * @property string created_at
 * @property string updated_at
 * @property User user
 */
class Participant extends Model implements AuditableContract, Presenter
{
    use Auditable, SoftDeletes, Presentable;

    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    const STATUS_PENDING = 2;

    const ARCHIVED = 1;
    const UNARCHIVED = 0;

    /**
     * The table name.
     * @var string
     */
    protected $table = 'core_participant';

    /**
     * The primary key.
     * @var string
     */
    protected $primaryKey = 'user_id';

    /**
     * Should the timestamps be audited?
     *
     * @var bool
     */
    protected $auditTimestamps = false;

    /**
     * Custom Audit Driver
     *
     * @var \Ignite\Core\Audit\ParticipantDriver
     */
    protected $auditDriver = \Ignite\Core\Audit\ParticipantDriver::class;

    /**
     * Participant presenter.
     *
     * @var string
     */
    protected $presenter = \Ignite\Core\Presenters\Participant::class;

    /**
     * Attributes to exclude from the Audit.
     *
     * @var array
     */
    protected $auditExclude = [
        'id',
        'updated_at',
        'created_at'
    ];

    /**
     * Don't auto-increment, or set Primary Key = 0
     * @var boolean
     */
    public $incrementing = false;

    /**
     * Un-guard all attributes.
     * @var array
     */
    protected $guarded = [];

    /**
     * The participant status options.
     * @var array
     */
    public static $statuses = [
        self::STATUS_ACTIVE => 'active',
        self::STATUS_INACTIVE => 'inactive',
        self::STATUS_PENDING => 'pending',
    ];

    /**
     * Get a list of Participant statuses.
     *
     * @return array
     */
    public static function getStatusList()
    {
        return self::$statuses;
    }

    /**
     * The relationship to the User model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * The relationship to the AppNotes model.
     *
     * @return mixed
     */
    public function notes()
    {
        return $this->hasMany(Note::class, 'table_primary_id', 'user_id')
            ->whereIn('table_name', ['user', 'participant'])
            ->orderBy('created_at', 'desc');
    }

    /**
     * The relationship to transactions.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function transactions()
    {
        return $this->hasMany(Transaction::class, 'user_id', 'user_id');
    }

    /**
     * Scope a query by email.
     *
     * @param Builder $query
     * @param string  $email
     */
    public function scopeByEmail(Builder $query, $email)
    {
        $query->where('email', $email);
    }

    /**
     * The participant's full name.
     *
     * @return string
     */
    public function fullName()
    {
        return optional($this)->first . ' ' . optional($this)->last;
    }

    /**
     * The identifier for the record in the audit log.
     *
     * @return mixed
     */
    public static function getAuditFriendlyField()
    {
        return ['first', 'last'];
    }
}
