<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class NikRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        // The NIK is a 16-digit number
        if (!preg_match('/^[0-9]{16}$/', $value)) {
            return false;
        }

        // // The first 6 digits represent the person's birth date in the format of YYMMDD
        // $year = substr($value, 6, 2);
        // $month = substr($value, 8, 2);
        // $day = substr($value, 10, 2);
        
        // // dd($year, $month, $day);
        // // dd(checkdate($month, $day, "19{$year}"));

        // if (!checkdate($month, $day, "19{$year}")) {
        //     return false;
        // }

        // // The next 2 digits represent the place of birth (province/city code)
        // $provinceCode = substr($value, 6, 2);

        // // The next 2 digits represent the person's gender (odd for male, even for female)
        // $genderCode = substr($value, 14, 1);

        // // The last 4 digits represent the sequence number of the person's birth in that day
        // $sequenceNumber = substr($value, 12, 4);

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ':attribute bukan valid NIK Indonesia.';
    }
}
