<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Laravel\Sanctum\HasApiTokens;

class Partner extends Authenticatable
{
    use HasApiTokens;

    protected $guarded = [];
    protected $table = 'partners';

    protected $appends = ['age'];

    const MAIN_COACHES = 1;
    const COACHES = 2;
    const CUSTOMER_SERVICE_USER = 3;
    const CHEF_USER = 6;
    const ROLE = [
        self::MAIN_COACHES => 'main_coaches',
        self::COACHES => 'coaches',
        self::CUSTOMER_SERVICE_USER => 'customer_service_user',
        self::CHEF_USER => 'chef_user',
    ];

    protected function age(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                if(!isset($attributes['birthday'])){
                    return null;
                }
                $birthday = $attributes['birthday'];

                if ($birthday == 0 || empty($birthday)) {
                    return null; // 或者返回其他默认值
                }

                return date('Y') - date('Y', strtotime($birthday));
            }
//            get: fn ($value,$attributes) => date('Y') - date('Y',strtotime($attributes['birthday']??now())),
        );
    }

    public function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected function specialty(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => json_decode($value),
        );
    }

    protected function protocols(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => json_decode($value),
        );
    }

    public function agencies()
    {
        return $this->hasMany(Agency::class, 'partner_id', 'id')->orderBy('id', 'asc');
    }

    public function offline_order()
    {
        return $this->hasMany(OfflineOrder::class);
    }

    public function coach()
    {
        return $this->hasOne(Coach::class);
    }

    public function service_user(){
        return $this->hasOne(ServiceUser::class,'user_id','user_id');
    }

    public function service_roles(){
        return $this->belongsToMany(ServiceRole::class,'service_role_users','user_id','role_id','user_id','id')->withTimestamps();
    }

    public function wechat(){
        return $this->hasOne(Wechat::class,'openid','openid');
    }

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

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->where(function ($sql) use($keyword) {
                $sql->where("name", 'like', '%'.trim($keyword).'%')
                    ->orWhere('mobile', 'like', '%'.trim($keyword).'%');
            });
        }
        return $query;
    }

    public function scopeHasUser($query)
    {
        return $query->whereHas('user');
    }

    public function scopeStatus($query){
        $status = request()->status;
        if (is_numeric($status)) {
            return $query->where('status', $status);
        }
        return $query;
    }
}
