<?php

namespace Mehrab\Media\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Mehrab\City\Http\Requests\city_store_request;
use Mehrab\City\Http\Requests\city_update_request;
use Mehrab\City\Models\City;
use Mehrab\City\Repositories\CityRepo;
use Mehrab\Media\Models\Media;
use Mehrab\Media\Services\MediaFileService;
use Mehrab\OrderService\Models\OrderService;
use Mehrab\OrderService\Repositories\OrderServiceRepo;
use Mehrab\RolePermission\Models\Permission;


class MediaController extends Controller
{
    private $Media;

    public function __construct(Media $media)
    {
        $this->Media = $media;
    }

    public function download_order_media(\Illuminate\Http\Request $request, Media $media, $orderID)
    {
        if (!$request->hasValidSignature()) {
            abort(401);
        }
        $user = auth()->user();
        $order = (new OrderServiceRepo())->findByID($orderID);
        if ($user->hasPermissionTo(Permission::PERMISSION_SUPER_ADMIN) ||
            $user->hasPermissionTo(Permission::PERMISSION_ACCESS_ALL_ORDER_SERVICE) ||
            $user->hasPermissionTo(Permission::PERMISSION_ORDER_SERVICE_FACTOR) ||
            $user->id == $order->expert_id ||
            $user->id == $order->operator_id ||
            $user->id == $order->team_leader_id
        ) {
            MediaFileService::stream($media);
        }
        return false;
    }

    public function download_national_card_pic_media(\Illuminate\Http\Request $request, Media $media)
    {
        if (!$request->hasValidSignature()) {
            abort(401);
        }
        $user = auth()->user();
        if ($user->hasPermissionTo(Permission::PERMISSION_SUPER_ADMIN) ||
            $user->hasPermissionTo(Permission::PERMISSION_MANAGE_USER)
        ) {
            MediaFileService::stream($media);
        }
        return false;
    }


    public function download_certificate_pic_media(\Illuminate\Http\Request $request, Media $media)
    {
        if (!$request->hasValidSignature()) {
            abort(401);
        }
        $user = auth()->user();
        if ($user->hasPermissionTo(Permission::PERMISSION_SUPER_ADMIN) ||
            $user->hasPermissionTo(Permission::PERMISSION_MANAGE_USER)
        ) {
            MediaFileService::stream($media);
        }
        return false;
    }

}
