<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * @property Carbon $created_at
 * @property int $stop_part
 * @property int $start_part
 * @property int $startSec
 * @property int $stopSec
 */
class ProgramVideos extends Model
{
    protected $fillable = ["video_bvid", "start_part", "start_time", "stop_part", "stop_time"];
    public function program(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Programs::class, "program_id", "id");
    }

    public function video(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Videos::class, "video_bvid", "bvid");
    }

    public function startSec(): Attribute
    {
        return Attribute::get(function ($_, $attributes) {
            if (!isset($attributes['start_time'])) {
                return "";
            }
            return Carbon::createFromFormat("H:i:s", $attributes['start_time'])->secondsSinceMidnight();
        });
    }

    public function stopSec(): Attribute
    {
        return Attribute::get(function ($_, $attributes) {
            if (!isset($attributes['stop_time'])) {
                return "";
            }
            return Carbon::createFromFormat("H:i:s", $attributes['stop_time'])->secondsSinceMidnight();
        });
    }
}