<?php

namespace AvoRed\Framework\Database\Repository;

use AvoRed\Framework\Database\Models\Property;
use AvoRed\Framework\Database\Contracts\PropertyModelInterface;
use AvoRed\Framework\Database\Traits\FilterTrait;
use Illuminate\Support\Collection;

class PropertyRepository extends BaseRepository implements PropertyModelInterface
{
    use FilterTrait;

    /**
     * Filterable Fields
     * @var array $filterType
     */
    protected $filterFields = [
        'name',
        'slug'
    ];


    /**
     * @var Property $model
     */
    protected $model;

    /**
     * Construct for the Property Repository
     */
    public function __construct()
    {
        $this->model = new Property();
    }

    /**
     * Get the model for the repository
     * @return Property
     */
    public function model(): Property
    {
        return $this->model;
    }

    /**
     * Get all the properties which should apply to all the products.
     * @return \Illuminate\Support\Collection $products
     */
    public function getAllProductProperties(): Collection
    {
        return $this->model()->where('use_for_all_products', true)->get();
    }


    public function savePropertyDropdown($request , $property)
    {
        if (($request->get('field_type') === 'RADIO' || $request->get('field_type') === 'SELECT')) {
            $property->dropdownOptions()->delete();
        }
        if (($request->get('field_type') === 'RADIO' ||
            $request->get('field_type') === 'SELECT') &&
            count($request->get('dropdown_option')) > 0
        ) {
            foreach ($request->get('dropdown_option') as $key => $option) {
                if (empty($option)) {
                    continue;
                }

                if (is_string($key)) {
                    $property->dropdownOptions()->create(['display_text' => $option]);
                } else {
                    $optionModel = $property->dropdownOptions()->find($key);
                    $optionModel->update(['display_text' => $option]);
                }
            }
        }
    }

}
