<?php

namespace App\Http\Controllers;

use App\Http\Helpers\FileUploader;
use App\Models\Milestone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Inertia\Inertia;

class MilestoneController extends Controller
{

    public function pay(Milestone $milestone, Request $request)
    {
        if($request->client_signature){
            $request['client_signature'] = FileUploader::storeBase64('signatures/', $request->client_signature);
        }

        $session = $milestone->pay($request->client_signature,$request->use_wallet);

        return $session ? Inertia::location($session['url']) : back(303)->with(["success" => "success"]);

    }

    public function approve(Milestone $milestone)
    {
        $milestone->accept();

        return back(303)->with(['success'=>'success']);
    }

    public function addFiles(Request $request){
        Milestone::find($request->id)->addFiles($request->files);

        return back(303)->with('success');
    }
}
