<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

use App\Models\Supplier;

class Supply extends Controller
{
	// Accepts a site name, service id, and integer/array of supplier rows to
	// create

	public static function fetch($site) {
		return Http::post(config("services.$site.link"), ['key' =>
			config("services.$site.key"), 'action' => 'services']);
	}

	public static function smmkings($service_id, $id) {
		$services = Http::post(config("services.smmkings.link"), ['key' =>
			config("services.smmkings.key"), 'action' => 'services'])->json();

		foreach ($services as $service) {
			if ($service['service'] == $id){
				$s = new Supplier;
				$s->service_id = $service_id;
				$s->supplier = 'smmkings';
				$s->supplier_id = (int) $service['service'];
				$s->supplier_name = $service['name'];
				$s->min = (int) $service['min'];
				$s->max = (int) $service['max'];
				$s->cost = $service['rate'] * 100;
				$s->save();
				return $s;
			} 
		}
		
	}

	public static function smmworld($service_id, $id) {
		$services = Http::post(config("services.smmworld.link"), ['key' =>
			config("services.smmworld.key"), 'action' => 'services']);
		$services = $services['services'];

		foreach ($services as $service) {
			if ($service['id'] == $id){
				$s = new Supplier;
				$s->service_id = $service_id;
				$s->supplier = 'smmworld';
				$s->supplier_id = $service['id'];
				$s->supplier_name = $service['name'];
				$s->min = $service['min'];
				$s->max = $service['max'];
				$s->cost = $service['price_per_k'] * 100;
				$s->save();
				return $s;
			} 
		}
	}

}