<commit_before><?php
use \Tipszonen\Repository\CouponRepository;
class Coupon extends BaseModel {
    use CouponRepository;

    protected $fillable = [];
    protected static $rules = [
        'name' => 'required|alpha_num_spaces|min:3',
    ];

    public static function boot()
    {
        parent::boot();

        static::creating(function($model)
        {
            if($model->validate())
            {
                return $model;
            }

            return false;
        });
    }

    public function coupon_detail()
    {
        return $this->belongsTo('CouponDetail');
    }

    public function coupon_rows()
    {
        return $this->hasMany('CouponRow');
    }

    public static function customFind($id)
    {
        return static::with('coupon_detail', 'coupon_rows')->find($id);
    }

    public static function customFindFile($id)
    {
        return static::with('coupon_detail')->find($id);
    }

    public function get_rights($results, $row)
    {
        $row = explode(',', $row);

        $rights = 0;
        for($i = 0; $i<count($row); $i++)
        {
            if($row[$i] == $results[$i])
                $rights++;
        }
        return $rights;
    }

    public function get_best_rows($count, $results)
    {
        $unsorted_rows = [];

        if( ! $this->is_file() )
        {
            foreach($this->coupon_rows as $row)
            {
                $unsorted_rows[] = [
                    'row' => $row->row,
                    'rights' => $row->get_rights($results)
                ];
            }
        } else
        {
            foreach($this->getRowsFromFile($this->file_url) as $row)
            {
                $unsorted_rows[] = [
                    'row' => $row['row'],
                    'rights' => self::get_rights($results, $row['row'])
                ];
            }
        }
        $sorted_rows = tz_array_sort($unsorted_rows, 'rights');

        return array_slice($sorted_rows, 0, $count);
    }

    public function get_best_row_rights()
    {
        return $this->get_best_rows(1, $this->coupon_detail->get_row_result())[0]['rights'];
    }

    public function createCouponRows($rows)
    {
        $rows_to_add = [];
        foreach($rows as $row_to_add)
        {
            $rows_to_add[] = [
                'coupon_id' => $this->id,
                'row' => trim($row_to_add)
            ];
        }

        DB::table('coupon_rows')->insert($rows_to_add);
    }

    public function getRowsFromFile($file)
    {
        $file_path = public_path();
        $file_path .= $file;
        $txt_file = file_get_contents($file_path);
        $rows = explode("\n", $txt_file);
        $rows_to_return = [];
        array_shift($rows);

        foreach($rows as $row => $data)
        {
            //get row data
            $row_data = explode(',', $data);
            $row_value = "";
            for( $i = 0; $i < count($row_data); $i++)
            {
                if($i > 0)
                {
                    if($i == count($row_data) - 1)
                    {
                        $row_value .= $row_data[$i];
                    } else
                    {
                        $row_value .= $row_data[$i] . ',';
                    }
                }
            }
            if($row_value != "")
            {
                $rows_to_return[] = [
                    'system_type' => 'E',
                    'row' => $row_value
                ];
            }
        }

        return $rows_to_return;
    }

    public function getRowsFromCoupon($input)
    {
        dd($input);
    }

    public static function _create_new($product_id)
    {
        $file = "https://svenskaspel.se/xternal/XMLkupong.asp";
        $xml = new DOMDocument();
        if(!$xml->load($file."?produktid=".$product_id))
        {
            return false;
        }

        $data['product_name'] = $xml->getElementsByTagName("produktnamn")->item(0)->nodeValue;
        $round  = $xml->getElementsByTagName("omgang")->item(0)->nodeValue;
        $game_start = $xml->getElementsByTagName("spelstart")->item(0)->nodeValue;
        $game_stop = $xml->getElementsByTagName("spelstopp")->item(0)->nodeValue;
        $game_week = $xml->getElementsByTagName("spelvecka")->item(0)->nodeValue;
<commit_msg>minorfix, bug when a product_id has no active round to play<commit_after><?php
use \Tipszonen\Repository\CouponRepository;
class Coupon extends BaseModel {
    use CouponRepository;

    protected $fillable = [];
    protected static $rules = [
        'name' => 'required|alpha_num_spaces|min:3',
    ];

    public static function boot()
    {
        parent::boot();

        static::creating(function($model)
        {
            if($model->validate())
            {
                return $model;
            }

            return false;
        });
    }

    public function coupon_detail()
    {
        return $this->belongsTo('CouponDetail');
    }

    public function coupon_rows()
    {
        return $this->hasMany('CouponRow');
    }

    public static function customFind($id)
    {
        return static::with('coupon_detail', 'coupon_rows')->find($id);
    }

    public static function customFindFile($id)
    {
        return static::with('coupon_detail')->find($id);
    }

    public function get_rights($results, $row)
    {
        $row = explode(',', $row);

        $rights = 0;
        for($i = 0; $i<count($row); $i++)
        {
            if($row[$i] == $results[$i])
                $rights++;
        }
        return $rights;
    }

    public function get_best_rows($count, $results)
    {
        $unsorted_rows = [];

        if( ! $this->is_file() )
        {
            foreach($this->coupon_rows as $row)
            {
                $unsorted_rows[] = [
                    'row' => $row->row,
                    'rights' => $row->get_rights($results)
                ];
            }
        } else
        {
            foreach($this->getRowsFromFile($this->file_url) as $row)
            {
                $unsorted_rows[] = [
                    'row' => $row['row'],
                    'rights' => self::get_rights($results, $row['row'])
                ];
            }
        }
        $sorted_rows = tz_array_sort($unsorted_rows, 'rights');

        return array_slice($sorted_rows, 0, $count);
    }

    public function get_best_row_rights()
    {
        return $this->get_best_rows(1, $this->coupon_detail->get_row_result())[0]['rights'];
    }

    public function createCouponRows($rows)
    {
        $rows_to_add = [];
        foreach($rows as $row_to_add)
        {
            $rows_to_add[] = [
                'coupon_id' => $this->id,
                'row' => trim($row_to_add)
            ];
        }

        DB::table('coupon_rows')->insert($rows_to_add);
    }

    public function getRowsFromFile($file)
    {
        $file_path = public_path();
        $file_path .= $file;
        $txt_file = file_get_contents($file_path);
        $rows = explode("\n", $txt_file);
        $rows_to_return = [];
        array_shift($rows);

        foreach($rows as $row => $data)
        {
            //get row data
            $row_data = explode(',', $data);
            $row_value = "";
            for( $i = 0; $i < count($row_data); $i++)
            {
                if($i > 0)
                {
                    if($i == count($row_data) - 1)
                    {
                        $row_value .= $row_data[$i];
                    } else
                    {
                        $row_value .= $row_data[$i] . ',';
                    }
                }
            }
            if($row_value != "")
            {
                $rows_to_return[] = [
                    'system_type' => 'E',
                    'row' => $row_value
                ];
            }
        }

        return $rows_to_return;
    }

    public function getRowsFromCoupon($input)
    {
        dd($input);
    }

    public static function _create_new($product_id)
    {
        $file = "https://svenskaspel.se/xternal/XMLkupong.asp";
        $xml = new DOMDocument();
        if(!$xml->load($file."?produktid=".$product_id))
        {
            return false;
        }

        $product_name = $xml->getElementsByTagName("produktnamn")->item(0);
        
        if( isset( $product_name ) )
        {
            $data['product_name'] = $product_name->nodeValue;
        } else
        {
            return false;
        }

        $round  = $xml->getElementsByTagName("omgang")->item(0)->nodeValue;
        $game_start = $xml->getElementsByTagName("spelstart")->item(0)->nodeValue;
        $game_stop = $xml->getElementsByTagName("spelstopp")->item(0)->nodeValue;
        $game_week = $xml->getElementsByTagName("spelvecka")->item(0)->nodeValue;

{'fraction_non_alphanumeric': 0.14951895868704018, 'fraction_numerical': 0.0031692133559705717, 'mean_word_length': 1.5318051575931233, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 89, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1962047', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2647, 'n_words': 406}