<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\Rule;

class OrgMaster extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'org_master';
    protected $primaryKey = 'Org_Id';
    public $incrementing = true;
    protected $keyType = 'int';

    protected $fillable = [
        'Org_Name',
        'Org_Code',
        'Org_GST_No',
        'Org_TDS_No',
        'Org_PAN_No',
        'Org_Address',
        'Org_City',
        'Org_State',
        'Org_Pin',
        'Org_Email',
        'Org_Mobile',
        'Org_URL',
        'Org_Logo',
    ];

    protected $casts = [
        'Org_Id' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    // Accessor: Always return full URL for logo
    public function getOrgLogoAttribute($value)
    {
        return $value ? asset('storage/' . $value) : null;
    }

    // Validation rules for CREATE
    public static function createRules()
    {
        return [
            'Org_Name'   => 'required|string|max:100',
            'Org_Code'   => 'nullable|string|max:25|unique:org_master,Org_Code',
            'Org_GST_No' => 'nullable|string|max:50|unique:org_master,Org_GST_No',
            'Org_TDS_No' => 'nullable|string|max:50',
            'Org_PAN_No' => 'nullable|string|max:50|unique:org_master,Org_PAN_No',
            'Org_Address' => 'nullable|string',
            'Org_City'   => 'nullable|string|max:50',
            'Org_State'  => 'nullable|string|max:50',
            'Org_Pin'    => 'nullable|string|max:10',
            'Org_Email'  => 'nullable|email|max:50',
            'Org_Mobile' => 'nullable|string|max:15',
            'Org_URL'    => 'nullable|url|max:150',
            'Org_Logo'   => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048'
        ];
    }

    // Validation rules for UPDATE
    // public static function updateRules($id)
    // {
    //     return [
    //         'Org_Name'   => 'sometimes|required|string|max:100',
    //         'Org_Code'   => 'sometimes|nullable|string|max:25|unique:org_master,Org_Code,' . $id . ',Org_Id',
    //         'Org_GST_No' => 'nullable|string|max:50|unique:org_master,Org_GST_No,' . $id . ',Org_Id',
    //         'Org_TDS_No' => 'nullable|string|max:50',
    //         'Org_PAN_No' => 'nullable|string|max:50|unique:org_master,Org_PAN_No,' . $id . ',Org_Id',
    //         'Org_Address'=> 'nullable|string',
    //         'Org_City'   => 'nullable|string|max:50',
    //         'Org_State'  => 'nullable|string|max:50',
    //         'Org_Pin'    => 'nullable|string|max:10',
    //         'Org_Email'  => 'nullable|email|max:50',
    //         'Org_Mobile' => 'nullable|string|max:15',
    //         'Org_URL'    => 'nullable|url|max:150',
    //         'Org_Logo'   => 'nullable|string' // base64 or path for update
    //     ];
    // }

    // public static function updateRules($id)
    // {
    //     return [
    //         'Org_Name' => 'sometimes|nullable|string|max:100',

    // 'Org_Code' => [
    //     'nullable',
    //     Rule::unique('org_master', 'Org_Code')
    //         ->ignore($id, 'Org_Id')
    //         ->whereNull('deleted_at'),
    // ],


    //         'Org_GST_No' => [
    //             'nullable',
    //             'string',
    //             'max:50',
    //             Rule::unique('org_master', 'Org_GST_No')->ignore($id, 'Org_Id'),
    //         ],

    //         'Org_PAN_No' => [
    //     'nullable',
    //     Rule::unique('org_master', 'Org_PAN_No')
    //         ->ignore($id, 'Org_Id')
    //         ->whereNull('deleted_at'),
    // ],

    //         'Org_TDS_No' => 'nullable|string|max:50',
    //         'Org_Address' => 'nullable|string',
    //         'Org_City' => 'nullable|string|max:50',
    //         'Org_State' => 'nullable|string|max:50',
    //         'Org_Pin' => 'nullable|string|max:10',
    //         'Org_Email' => 'nullable|email|max:50',
    //         'Org_Mobile' => 'nullable|string|max:15',
    //         'Org_URL' => 'nullable|url|max:150',
    //         'Org_Logo' => 'nullable|string',
    //     ];
    // }





public static function updateRules($id)
{
    return [

        'Org_Name' => 'sometimes|nullable|string|max:100',

        'Org_Code' => [
            'nullable',
            Rule::unique('org_master', 'Org_Code')
                ->ignore($id, 'Org_Id')
                ->whereNull('deleted_at')
        ],

        'Org_GST_No' => [
            'nullable',
            Rule::unique('org_master', 'Org_GST_No')
                ->ignore($id, 'Org_Id')
                ->whereNull('deleted_at')
        ],

        'Org_PAN_No' => [
            'nullable',
            Rule::unique('org_master', 'Org_PAN_No')
                ->ignore($id, 'Org_Id')
                ->whereNull('deleted_at')
        ],

        'Org_TDS_No' => 'nullable|string|max:50',
        'Org_Address' => 'nullable|string',
        'Org_City' => 'nullable|string|max:50',
        'Org_State' => 'nullable|string|max:50',
        'Org_Pin' => 'nullable|string|max:10',
        'Org_Email' => 'nullable|email|max:50',
        'Org_Mobile' => 'nullable|string|max:15',
        'Org_URL' => 'nullable|url|max:150',
        'Org_Logo' => 'nullable',
    ];
}

}

