<?php

namespace App\DataTables;

use App\Models\Operatore;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Storage;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

class OperatoreDataTableEditor extends DataTablesEditor
{
  protected $model = Operatore::class;
  // protected $uploadDir = '';
  // protected $disk = 'modelliRicevute';

  protected $messages = [
    'nome.required' => 'Il nome è richiesto',
    'cognome.required' => 'Il cognome è richiesto',
    'email.required' => 'L\'email è richiesta',
    'email.unique' => 'L\'email deve essere unica',
  ];

  /**
  * Get create action validation rules.
  *
  * @return array
  */
  public function createRules(): array
  {
    return [
      'nome'  => 'required',
      'cognome' => 'required',
      'email' => 'required|email|unique:operatore,email',
      'telefono' => 'nullable',
      // 'username' => 'required|unique:operatore,username',
      'password' => 'nullable|sometimes',
      'is_attivo' => 'boolean',
    ];
  }

  public function createMessages(): array{
    return $this->messages;
  }

  /**
  * Get edit action validation rules.
  *
  * @param Model $model
  * @return array
  */
  public function editRules(Model $model): array
  {
    return [
      'nome'  => 'required',
      'cognome' => 'required',
      'email' => 'nullable|email',
      'telefono' => 'nullable',
      'password' => 'nullable|sometimes',
      // 'username' => 'required|unique:operatore,username',
      // 'password' => 'required',
      'is_attivo' => 'boolean',
    ];
  }

  public function editMessages(): array{
    return $this->messages;
  }

  /**
  * Get remove action validation rules.
  *
  * @param Model $model
  * @return array
  */
  public function removeRules(Model $model): array
  {
    return [];
  }

  public function creating(Model $model, array $data): array
  {
    // $model->roles()->sync([$data['ruolo_id']]);
    $data['is_attivo'] = isset($data['is_attivo']) ? true : false;
    if(!isset($data['password'])){
      $data['password'] = Str::random(8);
    }
    $data['username'] = $this->createUsername($data);
    return $data;
  }

  public function updating(Model $model, array $data): array
  {
    // dd($data['ruolo']);
    // $model->roles()->sync([$data['ruolo_id']]);
    $data['is_attivo'] = isset($data['is_attivo']) ? true : false;
    $data['username'] = $this->createUsername($data);
    return $data;
  }
  public function messages(): array
  {
    return $this->messages;
  }

  public function createUsername(array $data): string{
     $baseUsername = strtolower($data['nome']) . '.' . strtolower($data['cognome']);
     $username = $baseUsername;
     $i = 1;
     while (\App\Models\Operatore::where('username', $username)->exists()) {
         $username = $baseUsername . $i;
         $i++;
     }
     return $username;

  }

}
