<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\ShoppingUser
 *
 * @property int $id
 * @property int|null $auth_user_id
 * @property string|null $billing_salutation
 * @property string|null $billing_company
 * @property string|null $billing_firstname
 * @property string|null $billing_lastname
 * @property string|null $billing_address
 * @property string|null $billing_address_2
 * @property string|null $billing_zipcode
 * @property string|null $billing_city
 * @property int $billing_country_id
 * @property string|null $billing_phone
 * @property string|null $billing_email
 * @property int $accepted_data_checkbox
 * @property int $same_as_billing
 * @property string|null $shipping_salutation
 * @property string|null $shipping_company
 * @property string|null $shipping_firstname
 * @property string|null $shipping_lastname
 * @property string|null $shipping_address
 * @property string|null $shipping_address_2
 * @property string|null $shipping_zipcode
 * @property string|null $shipping_city
 * @property int $shipping_country_id
 * @property string|null $shipping_phone
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrder[] $Shopping_orders
 * @property-read int|null $shopping_orders_count
 * @property-read \App\Models\Country $billing_country
 * @property-read \App\Models\Country $shipping_country
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAcceptedDataCheckbox($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAuthUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingAddress2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingFirstname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingLastname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingSalutation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingZipcode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereSameAsBilling($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingAddress2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingFirstname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingLastname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingSalutation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingZipcode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int|null $orders
 * @property-read \App\Models\ShoppingOrder $shopping_order
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrder[] $shopping_orders
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereOrders($value)
 * @property int|null $abo_options
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAboOptions($value)
 * @property int|null $member_id
 * @property int|null $number
 * @property bool $is_like
 * @property array|null $notice
 * @property-read \App\User|null $auth_user
 * @property-read \App\User|null $member
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsLike($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereMemberId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereNotice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereNumber($value)
 * @property bool|null $has_buyed
 * @property bool|null $subscribed
 * @property int|null $wp_order_number
 * @property string|null $wp_order_date
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereHasBuyed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereSubscribed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereWpOrderDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereWpOrderNumber($value)
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property string|null $user_deleted_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereUserDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser withoutTrashed()
 * @property string|null $mode
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereMode($value)
 * @property bool|null $faker_mail
 * @property string|null $shipping_email
 * @property string|null $is_for
 * @property string|null $is_from
 * @property int|null $shopping_user_id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereFakerMail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsFor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsFrom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShoppingUserId($value)
 */
class ShoppingUser extends Model
{
    protected $table = 'shopping_users';

    use SoftDeletes;
    protected $dates = ['deleted_at'];



    protected $fillable = [
        'auth_user_id',
        'member_id',
        'number',
        'is_like',
        'billing_salutation',
        'billing_company',
        'billing_firstname',
        'billing_lastname',
        'billing_address',
        'billing_address_2',
        'billing_zipcode',
        'billing_city',
        'billing_country_id',
        'billing_phone',
        'billing_email',
        'faker_mail',
        'shipping_email',
        'accepted_data_checkbox',
        'same_as_billing',
        'shipping_salutation',
        'shipping_company',
        'shipping_firstname',
        'shipping_lastname',
        'shipping_address',
        'shipping_address_2',
        'shipping_zipcode',
        'shipping_city',
        'shipping_country_id',
        'shipping_phone',
        'has_buyed',
        'subscribed',
        'notice',
        'mode',
        'is_for',
        'is_from',
        'shopping_user_id',
        'wp_order_number',
        'wp_order_date',
    ];

    protected $casts = [
        'notice' => 'array',
        'is_like' => 'bool',
        'faker_mail' => 'bool',
        'accepted_data_checkbox' => 'bool',
        'same_as_billing' => 'bool',
        'has_buyed' => 'bool',
        'subscribed' => 'bool',
        'wp_order_number' => 'int',
    ];



    //can null
    public function member()
    {
        return $this->belongsTo('App\User','member_id');
    }
     public function auth_user()
     {
         return $this->belongsTo('App\User','auth_user_id');
     }
    public function billing_country()
    {
        return $this->belongsTo('App\Models\Country','billing_country_id');
    }

    public function shipping_country()
    {
        return $this->belongsTo('App\Models\Country','shipping_country_id');
    }

    public function shopping_orders()
    {
        return $this->hasMany('App\Models\ShoppingOrder','shopping_user_id');
    }

    public function shopping_order()
    {
        return $this->hasOne('App\Models\ShoppingOrder','shopping_user_id');
    }

    public function setNotice($key, $value){
        $notice = $this->notice;
        $notice[$key] = $value;
        $this->notice = $notice;
        $this->save();
    }

    public function getNotice($key){
        return isset($this->notice[$key]) ? $this->notice[$key] : false;
    }

    public function removeNotice($key){
        $notice = $this->notice;
        if(isset($notice[$key])){
            unset($notice[$key]);
        }
        $this->notice = $notice;
        $this->save();
    }

    public function firstEntryByNumber(){

        if($this->number>0){
            if($shopping_user = ShoppingUser::where('number', $this->number)->orderBy('created_at', 'ASC')->first()){
                return $shopping_user;
            }
        }
        return $this;
    }
    public function lastEntryByNumber(){

        if($this->number>0){
            if($shopping_user = ShoppingUser::where('number', $this->number)->orderBy('created_at', 'DESC')->first()){
                return $shopping_user;
            }
        }
        return $this;
    }

    public function getAPIShippedType() {
        if($this->shopping_order){
            return $this->shopping_order->getAPIShippedType();
        }
        return "free";
    }
}