<?php
  namespace App\Services;

  use App\Models\User;
  use App\Models\Classe;
  use App\Models\Document;
  use App\Models\CeActivite;
  use App\Models\UpActivite;
  use App\Models\Responsible;
  use App\Models\Introductif;
  use App\Models\Inscription;
  use App\Models\SchoolMatter;
  use App\Models\ReportQuarter;
  use App\Models\DirecteurVisit;
  use App\Models\AnimateurVisit;
  use App\Models\CuttingSchoolYear;
  use Illuminate\Support\Str;
  class RepportService
  {
    public function introductif($data) {
      $exist = false;
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $content = trim($data['text']);
        $exist = Introductif::where('report_quarter_id', $report)->first();
        if(!$exist) {
          $exist = Introductif::create([
            'content' => strtolower($content),
            'report_quarter_id' => $report
          ]);
        }
        else {
          $exist->update(['content' => strtolower($content)]);
        }
      }
      return $exist ? true:false;
    }

    public function document($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveDoc($data['docs'], $data['nbre'], $data['text'], $report);
      }
      $exist = $report ? Document::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }

    public function responsable($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveRespo($data['users'], $data['matters'], $report);
      }
      $exist = $report ? Responsible::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }

    public function upActivite($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveUp($data['activite'], $data['theme'], $data['select'], $data['time'], $data['date'], $data['content'], $report);
      }
      $exist = $report ? UpActivite::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function CeActivite($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveCE($data['activite'], $data['theme'], $data['select'], $data['time'], $data['date'], $data['content'], $report);
      }
      $exist = $report ? CeActivite::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function directeur($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveDerict($data['text'], $data['user'], $data['matter'], $data['time'], $data['date'], $data['classe'], $report);
      }
      $exist = $report ? DirecteurVisit::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function animateur($data) {
      $report = $this->verify($data['school'], $data['cutting']);
      if($report){
        $this->saveAnim($data['text'], $data['user'], $data['matter'], $data['time'], $data['date'], $data['classe'], $report);
      }
      $exist = $report ? AnimateurVisit::where('report_quarter_id', $report)->count():false;
      return $exist ? true:false;
    }


    public function getData($school) {
      $users = User::where('school_id', $school)->where('role_id', '6')->where('status', '1')->orderBy('first_name')->orderBy('last_name')->get();
      $matters = SchoolMatter::join('matters', 'matters.id', 'school_matter.matter_id')
      ->select('school_matter.id', 'matters.libelle', 'matters.abbreviated')
      ->where('school_matter.school_id', $school)
      ->where('school_matter.type', 'secondaire')
      ->where('matters.libelle', '!=', 'conduite')
      ->orderBy('matters.libelle')
      ->get();
      return [$users, $matters];
    }


    public function getstudent($class) {
      $dts = Inscription::join('students', 'students.id', 'inscriptions.student_id')
      ->select('inscriptions.id', 'students.first_name', 'students.last_name')
      ->where('inscriptions.classe_id', $class)
      ->orderBy('students.first_name')
      ->orderBy('students.last_name')
      ->get()->map(function ($dt) {
        $dt->first_name = Str::upper($dt->first_name);
        $dt->last_name = ucwords($dt->last_name);
        return $dt;
      });
      return $dts ?? [];
    }


    public function getClass($school, $cutting) {
      $cutting = CuttingSchoolYear::find($cutting);
      $dts = Classe::where('school_id', $school)->where('school_year_id', $cutting->school_year_id)->where('type', 'secondaire')->orderBy('level_id')->get();
      return $dts ?? [];
    }

    
    private function verify($school, $cutting) {
      $exist = ReportQuarter::where('school_id', $school)->where('cutting_school_year_id', $cutting)->first();
      if(!$exist) {
        $exist = ReportQuarter::create([
          'school_id' => $school,
          'cutting_school_year_id' => $cutting
        ]);
      }
      return $exist ? $exist->id:null;
    }

    private function saveDoc($type, $nbre, $content, $report) {
      $i = 0;
      while($i < count($content)) {
        $data = Document::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'type' => trim(strtolower($type[$i])),
            'nomber' => $nbre[$i],
            'content' => trim(strtolower($content[$i]))
          ]);
        }
        else{
          Document::create([
            'order' => $i+1,
            'type' => trim(strtolower($type[$i])),
            'nomber' => $nbre[$i],
            'content' => trim(strtolower($content[$i])),
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }

    private function saveRespo($users, $matters, $report) {
      $i = 0;
      while($i < count($users) ) {
        $data = Responsible::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'user_id' => $users[$i],
            'school_matter_id' => $matters[$i]
          ]);
        }
        else{
          Responsible::create([
            'order' => $i+1,
            'user_id' => $users[$i],
            'school_matter_id' => $matters[$i],
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }

    private function saveUp($activit, $theme, $select, $time, $date, $text, $report) {
      $i = 0;
      while($i < count($activit) ) {
        $data = UpActivite::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'time' => $time[$i],
            'date' => $date[$i],
            'school_matter_id' => $select[$i],
            'texte' => trim(strtolower($text[$i])),
            'theme' => trim(strtolower($theme[$i])),
            'activite' => trim(strtolower($activit[$i]))
          ]);
        }
        else{
          UpActivite::create([
            'order' => $i+1,
            'time' => $time[$i],
            'date' => $date[$i],
            'school_matter_id' => $select[$i],
            'texte' => trim(strtolower($text[$i])),
            'theme' => trim(strtolower($theme[$i])),
            'activite' => trim(strtolower($activit[$i])),
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }


    private function saveCe($activit, $theme, $select, $time, $date, $text, $report) {
      $i = 0;
      while($i < count($activit) ) {
        $data = CeActivite::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'lieu' => $time[$i],
            'date' => $date[$i],
            'school_matter_id' => $select[$i],
            'texte' => trim(strtolower($text[$i])),
            'theme' => trim(strtolower($theme[$i])),
            'activite' => trim(strtolower($activit[$i]))
          ]);
        }
        else{
          CeActivite::create([
            'order' => $i+1,
            'lieu' => $time[$i],
            'date' => $date[$i],
            'school_matter_id' => $select[$i],
            'texte' => trim(strtolower($text[$i])),
            'theme' => trim(strtolower($theme[$i])),
            'activite' => trim(strtolower($activit[$i])),
            'report_quarter_id' => $report
          ]);
        }
        $i++;
      }
    }


    private function saveDerict($text, $user, $matter, $time, $date, $class, $report) {
      $i = 0;
      while($i < count($text) ) {
        $data = DirecteurVisit::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'qualite' => trim(strtolower($text[$i])),
            'school_matter_id' => $matter[$i],
            'classe_id' => $class[$i],
            'user_id' => $user[$i],
            'time' => $time[$i],
            'date' => $date[$i]
          ]);
        }
        else{
          DirecteurVisit::create([
            'qualite' => trim(strtolower($text[$i])),
            'school_matter_id' => $matter[$i],
            'report_quarter_id' => $report,
            'classe_id' => $class[$i],
            'user_id' => $user[$i],
            'time' => $time[$i],
            'date' => $date[$i],
            'order' => $i+1
          ]);
        }
        $i++;
      }
    }

    public function saveAnim($text, $user, $matter, $time, $date, $class, $report) {
      $i = 0;
      while($i < count($text) ) {
        $data = AnimateurVisit::where('report_quarter_id', $report)->where('order', $i+1)->first();
        if($data){
          $data->update([
            'qualite' => trim(strtolower($text[$i])),
            'school_matter_id' => $matter[$i],
            'classe_id' => $class[$i],
            'user_id' => $user[$i],
            'time' => $time[$i],
            'date' => $date[$i]
          ]);
        }
        else{
          AnimateurVisit::create([
            'qualite' => trim(strtolower($text[$i])),
            'school_matter_id' => $matter[$i],
            'report_quarter_id' => $report,
            'classe_id' => $class[$i],
            'user_id' => $user[$i],
            'time' => $time[$i],
            'date' => $date[$i],
            'order' => $i+1
          ]);
        }
        $i++;
      }
    }
  }

