<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use App\Models\AdminNotification;

use App\Models\EmailQueue;
use App\Models\User;

use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Mail;

class MailQueueController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');

        $adminUser = auth()->guard('admin')->user();

        if (!$adminUser)
        {
            return response()->json(['error' => 'Unauthorized.'], 401);
        }

    }

    /**
     * Tipi di mail
     */
    public function mail_type($type, $mode='')
    {
        $mail_type = [];

        if ($mode == 'id') {
            $mail_type['NEW_USER'] = 0;
            $mail_type['NEW_COMPANY'] = 1;
            $mail_type['ACCOUNT_EXPIRED_12'] = 2;
            $mail_type['INACTIVE_PN'] = 3;
            $mail_type['PN_REJECTED'] = 4;
            $mail_type['PN_MODIFIED'] = 5;
        }else{

            $mail_type[0] = 'NEW_USER';
            $mail_type[1] = 'NEW_COMPANY';
            $mail_type[2] = 'ACCOUNT_EXPIRED_12';
            $mail_type[3] = 'INACTIVE_PN';
            $mail_type[4] = 'PN_REJECTED';
            $mail_type[5] = 'PN_MODIFIED';
        }

        if (array_key_exists($type, $mail_type)) {
            return $mail_type[$type];
        }else{
            return false;
        }
    }
    /**
     *  Salva la mail nella queue 
     */
    public function insertMailToQueue(Request $request)
    {
        $mail_to_insert = [
            "label" => $request->label,
            "type" => $this->mail_type($request->type),
            "status" => '',
            "info" => '',
            "mail_json" => $request->body,
            "to" => $request->to,
            "from" => $request->from,
            "cc" => $request->cc,
            "ccn" => $request->ccn,
            "send_at" => Carbon::now()
        ];

        $mail = EmailQueue::create[ $mail_to_insert ];
          
    }

    /**
     * Controlla le mail da mandare
     */
    public function checkMAilToSend(Request $request)
    {}
    
    /**
     * Manda le mail in queue
     */
    public function sendMailQueue(Request $request)
    {}

    /**
     * Manda una singola mail
     */
    public function sendSingleMail($mail_obj)
    {}

}
