<?php

namespace Modules\Feature\Repositories;



use Modules\Feature\Interfaces\FeatureRepositoryInterface;
use Modules\Feature\Models\Feature;
use Modules\Feature\Models\FeatureDescription;

class FeatureRepository implements FeatureRepositoryInterface
{
    public function get(array $request)
    {
        return Feature::query();
    }

    public function create(array $request)
    {
        foreach ($request['features'] as $row)
        {
            $feature = Feature::create([
                'image'=>$row['image'] ?? null,
                'featureable_id'=>$request['id'],
                'featureable_type'=>$request['type']
            ]);
            foreach (languages() as $lang)
            {
                $data=[
                    'feature_id'=> $feature->id,
                    'language_id'=> $lang->id,
                    'title'=> $row['title_'.$lang->local],
                    'description'=> $row['description_'.$lang->local],
                ];
                FeatureDescription::create($data);
            }
        }
    }


    public function deleteAll(array $request): void
    {
        Feature::where('featureable_id',$request['id'])->where('featureable_type',$request['type'])->delete();
    }
}
