<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class BookmarkSite extends Model {
    use HasFactory;
    protected $table = "bookmark__sites";
    protected $fillable = ['name', 'description', 'url', 'category'];

    public function category(): BelongsTo {
        return $this->belongsTo(BookmarkCategory::class, 'category');
    }

    public static function create(Request $request): BookmarkSite {
        $category = BookmarkCategory::where('id', $request->get('category'))->firstOrFail();
        $newSite = new BookmarkSite;
        $newSite->name = $request->get('name');
        $newSite->url = $request->get('url');
        $newSite->description = $request->get('description');
        $newSite->category = $category->id;
        $newSite->save();
        return $newSite;
    }

    public static function insertBookmark(string $name, string $url, int $category): void {
        $category = BookmarkCategory::where('id', $category)->firstOrFail();
        $newBookmark = new BookmarkSite;
        $newBookmark->name = $name;
        $newBookmark->url = $url;
        $newBookmark->category = $category->id;
        $newBookmark->save();
    }
}
