<?php

namespace App\Models;

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

class Notes extends Model
{
    use HasFactory;

    protected $fillable = [
        'n_user_id', 'n_day', 'n_month', 'n_year', 'n_time_type', 'n_reason', 'n_note',
    ];

    /**
     * Lấy thông tin bảng notes dựa vào tháng và năm.
     *
     * @param int $month
     * @param int $year
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function getNotesByMonthAndYear($month, $year)
    {
        return self::leftJoin("categories as reason", function ($join) {
            $join->on('n_reason', '=', 'reason.c_code');
            $join->on('reason.c_type', DB::raw("CONCAT('REASON')"));
        })
            ->leftJoin("categories as timeTypes", function ($join) {
                $join->on('n_time_type', '=', 'timeTypes.c_code');
                $join->on('timeTypes.c_type', DB::raw("CONCAT('TIME_TYPE')"));
            })
            ->where('n_month', $month)
            ->where('n_year', $year)
            ->select(
                'notes.id as n_id',
                'n_user_id',
                'n_day',
                'n_month',
                'n_year',
                'n_time_type',
                'n_reason',
                'n_note',
                'reason.c_name as reason_name',
                'timeTypes.c_name as time_type_name'
            )
            ->get();
    }
}
