<?php

namespace Botble\Ecommerce\Models;

use Botble\Base\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\DB;

class Option extends BaseModel
{
    protected $table = 'ec_options';

    protected $fillable = [
        'name',
        'option_type',
        'required',
        'product_id',
        'order',
    ];

    protected static function booted(): void
    {
        self::deleted(function (Option $option): void {
            $option->values()->delete();
            $option->translations()->delete();
        });
    }

    public function values(): HasMany
    {
        return $this
            ->hasMany(OptionValue::class, 'option_id')
            ->oldest('order');
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function translations(): HasMany
    {
        return $this->hasMany(OptionTranslation::class, 'ec_options_id');
    }

    public function getTranslation(string $langCode): ?OptionTranslation
    {
        return $this->translations->firstWhere('lang_code', $langCode);
    }

    public function saveTranslation(string $langCode, string $name): void
    {
        DB::table('ec_options_translations')->updateOrInsert(
            [
                'ec_options_id' => $this->id,
                'lang_code' => $langCode,
            ],
            ['name' => $name]
        );
    }
}
