<?php

namespace App\Models;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Parents extends Model
{
    use HasFactory;


    public static function createAndGetId(array $data)
    {
        $newParentId = 0;
        DB::transaction(function () use ($data, &$newParentId) {

            $newParentId = DB::table('parents')->insertGetId([
                'first_name' => $data['first_name'],
                'last_name' => $data['last_name'],
                'job' => $data['job'],
                'phone' => $data['phone'],
                'gender' => $data['gender'],
                "created_at" =>  \Carbon\Carbon::now(),
                "updated_at" => \Carbon\Carbon::now(),
            ]);


            $username =  strtolower(Str::random(8));
            $pass =  strtolower(Str::random(8));

            while (DeactivatedParentsAccount::where('username', $username)->exists()) {
                $username = strtolower(Str::random(8));
            }

            DeactivatedParentsAccount::create([
                'username' => $username,
                'password' => $pass,
                'parent_id' => $newParentId,
            ]);
        });

        return $newParentId;
    }


    public function getParentsByStudentsDivisions($divisions)
    {
        return $this->select('parents.*')->join('students', function ($join) {
            $join->on('students.father_id', '=', 'parents.id')
                ->orOn('students.mother_id', '=', 'parents.id');
        })->whereIn('students.division_id', $divisions)->get();
    }


    public function getParentsDivisions()
    {
        $parentDivisionsData = $this->select('students.division_id')->distinct()->join('students', function ($join) {
            $join->on('students.father_id', '=', "parents.id")
                ->orOn('students.mother_id', '=', "parents.id");
        })->where('parents.id', $this->id)->get()->toArray();

        $parentDivisions = [];
        foreach ($parentDivisionsData as  $value) {
            $parentDivisions[] = $value['division_id'];
        }
        return $parentDivisions;
    }

    public static function getParentNotifications($parent_account_id)
    {
        return DB::table('parent_notifications')
            ->select('payload', 'message', 'title', 'read_at', 'created_at')
            ->where('parent_account_id', $parent_account_id)
            ->take(20)
            ->orderBy('created_at', 'desc')->get();
    }



    public static function getParentsStages($parent_id)
    {
        $parentStagesData = self::select('levels.stage_id')->distinct()
            ->join('students', function ($join) {
                $join->on('students.father_id', '=', "parents.id")
                    ->orOn('students.mother_id', '=', "parents.id");
            })
            ->join('divisions', 'divisions.id', '=', 'students.division_id')
            ->join('levels', 'levels.id', '=', 'divisions.level_id')
            ->where('parents.id', $parent_id)->get()->toArray();

        $parentStages = [];
        foreach ($parentStagesData as  $value) {
            $parentStages[] = $value['stage_id'];
        }
        return $parentStages;
    }


    public static function getParentsByStage($stage_id)
    {
        $query = self::select('parents.*')->distinct()
            ->join('students', function ($join) {
                $join->on('students.father_id', '=', "parents.id")
                    ->orOn('students.mother_id', '=', "parents.id");
            });
        if ($stage_id) {
            $query = $query->join('divisions', 'divisions.id', '=', 'students.division_id')
                ->join('levels', 'levels.id', '=', 'divisions.level_id')
                ->where('levels.stage_id', $stage_id);
        }

        $stageParent = $query->get();

        return $stageParent;
    }



    public static function getParentAnnouncements($parent_id)
    {
        return DB::table('announcements')
            ->select('id', 'title', 'text', 'created_at')
            ->where('stage_id', self::getParentsStages($parent_id))
            ->orWhereNull('stage_id')
            ->orderBy('created_at', 'desc')->take(30)->get();
    }
}
