<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\Service;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class OrderController extends Controller
{
	//THis should also reduce user's available credits
	public function newOrder(Request $request) {
		$user = Auth::user();
		$order = new Order;

		$order->service_id = $request->service;
		$order->user_id = Auth::user()->id;
		$order->quantity = $request->quantity;
		$order->url = $request->url;
		$order->note = $request->note ?: '';
		$order->status = 'processing';

		$cost = ceil($order->quantity*$order->service->price/1000);
		$user->credits = $user->credits - $cost;
		if ($cost > $user->credits) {
			abort(520, 'Insufficient Credits');
		}
		$user->save();
		$order->save();
	}
}
