<?php

namespace Modules\Task\Models;

use App\Models\Model;
use App\Traits\HasCovers;
use App\Traits\HasStatus;
use App\Traits\WithPosition;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\Linker\Traits\HasLinker;
use Modules\Task\Models\Traits\TaskAttribute;
use Modules\User\Models\AccountRule;
use Modules\User\Models\Identity;
use Modules\User\Models\User;
use Modules\Task\Models\User as TaskUser;

class Task extends Model
{

    use HasLinker,
        HasCovers,
        WithPosition,
        TaskAttribute,
        HasStatus;

    const CYCLE_DAY   = 'day';
    const CYCLE_WEEK  = 'week';
    const CYCLE_MONTH = 'month';
    const CYCLE_YEAR  = 'year';
    const CYCLE_ONE   = 'one';

    const CYCLES = [
        self::CYCLE_DAY   => '日',
        self::CYCLE_WEEK  => '周',
        self::CYCLE_MONTH => '月',
        self::CYCLE_YEAR  => '年',
        self::CYCLE_ONE   => '一次',
    ];

    const TYPE_SUM   = 'sum';
    const TYPE_COUNT = 'count';

    const TYPES = [
        self::TYPE_SUM   => '和值',
        self::TYPE_COUNT => '计数',
    ];

    const COMPANY_CERTIFICATION_YES = 1;
    const COMPANY_CERTIFICATION_NO  = 0;

    const COMPANY_CERTIFICATIONS = [
        self::COMPANY_CERTIFICATION_YES => '是',
        self::COMPANY_CERTIFICATION_NO  => '否',
    ];

    const COMPANY_CERTIFICATION_LABEL = [
        self::COMPANY_CERTIFICATION_YES => 'info',
        self::COMPANY_CERTIFICATION_NO  => 'success',
    ];

    const POSITION_RECOMMENDED = 1;

    public array $position_map = [
        self::POSITION_RECOMMENDED => '推荐',
    ];

    public string $cover_field = 'cover';

    public $casts = [
        'identity' => 'json',
    ];


    const STATUS_DOWN = 0;
    const STATUS_UP   = 1;

    public array $status_map = [
        self::STATUS_UP   => '开启',
        self::STATUS_DOWN => '关闭',
    ];

    public function getIcoUrlAttribute(): string
    {
        return $this->parseImageUrl($this->ico);
    }

    /**
     * Notes: 关联分类
     *
     * @Author: 玄尘
     * @Date: 2022/10/19 16:45
     * @return BelongsTo
     */
    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * Notes: 关联规则
     *
     * @Author: 玄尘
     * @Date  : 2021/9/30 14:30
     * @return BelongsTo
     */
    public function rule(): BelongsTo
    {
        return $this->belongsTo(AccountRule::class, 'rule_id');
    }

    /**
     * Notes: 获取账变值
     *
     * @Author: 玄尘
     * @Date: 2022/10/20 11:05
     */
    public function getRuleNumber()
    {
        $variable = $this->rule_number;
        if (! $variable) {
            $variable = $this->rule ? $this->rule->variable : 0;
        }

        return $variable;
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date  : 2021/9/29 11:53
     * @param  Builder  $query
     * @param  User  $user
     * @return Builder
     */
    public function scopeOfUser(Builder $query, User $user): Builder
    {
        $identity = $user->identities->first();
        return $query->where('identity', 'like', "%$identity->id%");
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date  : 2021/9/29 11:53
     * @param  Builder  $query
     * @param  Identity  $identity
     * @return Builder
     */
    public function scopeOfIdentity(Builder $query, Identity $identity): Builder
    {
        return $query->where('identity', 'like', "%$identity->id%");
    }

    /**
     * Notes: 是否完成
     *
     * @Author: 玄尘
     * @Date  : 2021/9/30 13:25
     */
    public function getFinish($user): bool
    {
        $rule = $this->rule;
        if ($rule) {
            if (method_exists($this, $rule->name)) {
                return $this->{$rule->name}($user);
            }
        }

        return false;
    }

    /**
     * Notes: 任务数据
     *
     * @Author: 玄尘
     * @Date: 2022/10/20 10:43
     */
    public function taskUsers(): HasMany
    {
        return $this->hasMany(TaskUser::class);
    }

    /**
     * Notes: 任务记录
     *
     * @Author: 玄尘
     * @Date: 2022/10/20 10:45
     */
    public function taskLogs(): HasMany
    {
        return $this->hasMany(Log::class);
    }

    public function getIdentities()
    {
        return Identity::query()->whereIn('id',$this->identity)->pluck('name');
    }

}
