<?php

namespace App\Models;

use App\Casts\HttpToHttps;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Episode.
 *
 * @property int                               $id
 * @property int                               $category_id    分类
 * @property int                               $platform       所属平台
 * @property string                            $name           名称
 * @property string                            $cover_img      名称
 * @property int                               $status         状态 0-更新中 1-已完结
 * @property int                               $is_opend       上架状态 0-下架 1-上架
 * @property int                               $sort           排序
 * @property int                               $is_vip_watch   是否限定VIP观看
 * @property int                               $free_episodes  免费集数
 * @property int                               $paid_episodes  付费集数
 * @property string                            $episodes_price 单集价格设置
 * @property \Illuminate\Support\Carbon|null   $updated_at
 * @property \Illuminate\Support\Carbon|null   $deleted_at
 * @property \Illuminate\Support\Carbon|null   $created_at
 * @property \App\Models\EpisodesCategory|null $category
 * @property \App\Models\EpisodesList|null     $lists
 * @property int|null                          $lists_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Episode newQuery()
 * @method static \Illuminate\Database\Query\Builder|Episode onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|Episode query()
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereCoverImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereEpisodesPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereFreeEpisodes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereIsOpend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereIsVipWatch($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode wherePaidEpisodes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|Episode withTrashed()
 * @method static \Illuminate\Database\Query\Builder|Episode withoutTrashed()
 *
 * @mixin \Eloquent
 *
 * @property \App\Models\UserEpisodesRecord|null $userEpisodesRecords
 * @property int|null                            $user_episodes_records_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode wherePlatform($value)
 *
 * @property mixed                         $status_text
 * @property mixed                         $total
 * @property \App\Models\UserCollect|null  $userCollect
 * @property \App\Models\UserFavorite|null $userFavorite
 * @property int                           $share_count  分享数量
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereShareCount($value)
 *
 * @property \App\Models\UserWatchRecord|null $userWatchRecord
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode filterPlatform()
 *
 * @property int|null $favorite_num 虚拟点赞
 * @property int|null $collect_num  虚拟收藏
 * @property int|null $buy_num      虚拟购买
 * @property int|null $play_num     虚拟播放
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereBuyNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereCollectNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereFavoriteNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Episode wherePlayNum($value)
 *
 * @property int|null $virtual_share 虚拟分享
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Episode whereVirtualShare($value)
 */
class Episode extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $hidden = [
        'updated_at', 'deleted_at',
    ];

    protected $casts = [
        'platform'    => 'json',
        'share_count' => 'integer',
        'cover_img'   => HttpToHttps::class,
    ];

    public static function complete($episodes)
    {
        /* @var Episode $episode */
        foreach ($episodes as $episode) {
            $episode->status_text = $episode->status;
            $episode->total       = $episode->lists_count;
        }

        return $episodes;
    }

    public function scopeFilterPlatform($query)
    {
        return $query->where('platform', 'like', '%' . \user()->info->platform . '%');
    }

    public function category()
    {
        return $this->belongsTo(EpisodesCategory::class, 'category_id', 'id');
    }

    public function lists()
    {
        return $this->hasMany(EpisodesList::class, 'episodes_id', 'id');
    }

    public function setStatusTextAttribute($value)
    {
        $config                          = config('global.episode_status');
        $this->attributes['status_text'] = $config[$value] ?? '未知';
    }

    public function setTotalAttribute($value)
    {
        $this->attributes['total'] = $value;
    }

    /* 购买记录 */
    public function userEpisodesRecords()
    {
        return $this->hasMany(UserEpisodesRecord::class, 'episodes_id', 'id');
    }

    // 收藏
    public function userCollect()
    {
        return $this->belongsTo(UserCollect::class, 'id', 'episode_id');
    }

    // 喜欢
    public function userFavorite()
    {
        return $this->belongsTo(UserFavorite::class, 'id', 'episode_id');
    }

    // 观看记录
    public function userWatchRecord()
    {
        return $this->belongsTo(UserWatchRecord::class, 'id', 'episode_id');
    }
}
