<?php
namespace App\Model;
use App\Model\BaseModel;
use App\Core\Service\Logger;
use Exception;

class MasterDataRulesModel extends BaseModel
{
    public function  __construct()
    {
        parent::__construct();
    }

    public function getMasterTableRules($table_name = '', $action = 'insert', $data = [])
    {
        $rules = array();

        if (empty($table_name)) {
            return $rules;
        }
        if (!array_key_exists($table_name, $this->tables)) {
            return $rules;
        }

        if ($table_name == 'admin_users_master') {
            if ($action == 'insert') {
                $rules = array(
                    'name' => array("rules" => 'required|min_length[3]|max_length[50]', 'label' => "Name"),
                    'email_id' => array("rules" => 'required|valid_email', 'label' => "Email Id"),
                    'password' => array("rules" => 'required|min_length[6]|max_length[80]', 'label' => "Password"),
                    'is_master' => array("rules" => 'required|in_list[0,1]', 'label' => "Is Master"),
                );
            } elseif ($action == 'update') {
                $rules = array(
                    'name' => array("rules" => 'required|min_length[3]|max_length[50]', 'label' => "Name"),
                    'email_id' => array("rules" => 'required|valid_email', 'label' => "Email Id"),
                    'is_master' => array("rules" => 'required|in_list[0,1]', 'label' => "Is Master"),
                );
            }
        } elseif ($table_name == 'disposition_master') {
            $rules = array(
                'disposition_name' => array('rules' => 'required|min_length[2]|max_length[50]', 'label' => "Disposition name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
                'sub_dispositions' => array('rules' => 'min_length[2]', 'label' => "Sub disposition")
            );
            if ($action == 'update') {
                $rules['disposition_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'tax_master') {
            $rules = array(
                'tax_name' => array('rules' => 'required|min_length[2]|max_length[50]', 'label' => "Tax name"),
                'tax_status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
                'tax_type' => array('rules' => 'min_length[2]', 'label' => "Tax type")
            );
            if ($action == 'update') {
                $rules['tax_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'coupon_master') {
            $rules = array(
                'coupon_title' => array('rules' => 'required|min_length[2]|max_length[50]', 'label' => "Coupon title"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
                'valid_from' => array('rules' => 'required', 'label' => "Valid from date"),
                'valid_to' => array('rules' => 'required', 'label' => "Valid to date"),
                'coupon_count' => array('rules' => 'required', 'label' => "No. of coupon"),
                'min_order_amount' => array('rules' => 'required', 'label' => "Min. order amount"),
                'maximum_discount_amount' => array('rules' => 'required', 'label' => "Max. discount amount"),
                'coupon_type' => array('rules' => 'required', 'label' => "Coupon type")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'location_city') {
            $rules = array(
                'city_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "City name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
                'state_id' => array('rules' => 'required|min_length[1]|max_length[5]', 'label' => "State id")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'location_pincode') {
            $rules = array(
                'pincode' => array('rules' => 'required|min_length[6]', 'label' => "Pincode"),
                'state_id' => array('rules' => 'required|min_length[1]|max_length[5]', 'label' => "State"),
                'city_id' => array('rules' => 'required|min_length[1]|max_length[5]', 'label' => "City"),
                'origin_area' => array('rules' => 'max_length[3]', 'label' => "Origin area"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'customer_category_master') {
            $rules = array(
                'category_name' => array('rules' => 'required|min_length[2]', 'label' => "Category ame"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'customer_master') {
            $rules = array(
                'customer_name' => array('rules' => 'required|min_length[2]|max_length[200]', 'label' => "Category ame"),
                'mobile_number' => array('rules' => 'required|min_length[10]|max_length[15]', 'label' => "Mobile no."),
                'email_id' => array('rules' => 'valid_emails|min_length[2]|max_length[200]', 'label' => "Email"),
                'customer_type' => array('rules' => 'required', 'label' => "Customer type"),
                'branch_id' => array('rules' => 'required', 'label' => "Branch"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'lead_type') {
            $rules = array(
                'lead_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Lead name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'order_status') {
            $rules = array(
                'status_name' => array('rules' => 'required|min_length[2]|max_length[300]', 'label' => "Status name"),
                'color_code' => array('rules' => 'required', 'label' => "Color code"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'users_group_master') {
            $rules = array(
                'group_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Group name"),
                'order_status_id' => array('rules' => 'required', 'label' => "Order status"),
                'maximum_discount' => array('rules' => 'required', 'label' => "Max. discount"),
                'group_ip_address' => array('rules' => 'required|valid_ips', 'label' => "IP address"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'campaign_master') {
            $rules = array(
                'campaign_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Campaign name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['campaign_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'users_master') {
            $rules = array(
                'user_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "User name"),
                'user_login' => array('rules' => 'required', 'label' => "Login Id"),
                'sys_gen_password' => array('rules' => 'required|min_length[5]', 'label' => "Password"),
                'profile' => array('rules' => 'required', 'label' => "Profile"),
                'master_type' => array('rules' => 'in_list[0,1]', 'label' => "Master Type"),
                'orderby_change_rights' => array('rules' => 'in_list[0,1]', 'label' => "order book by rights"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['user_id'] = array('rules' => 'required', 'label' => 'Id');
                unset($rules['sys_gen_password']);
            }
        } elseif ($table_name == 'payment_type') {
            $rules = array(
                'payment_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Payment name"),
                'status_sequence' => array('rules' => 'required', 'label' => "Sequence"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'inventory_type') {
            $rules = array(
                'inventory_type_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Inventory type name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'invoice_type') {
            $rules = array(
                'title' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Title"),
                'inv_prefix' => array('rules' => 'required|min_length[2]|max_length[80]', 'label' => "Prefix"),
                'min_length' => array('rules' => 'required|max_length[10]', 'label' => "Min inv length"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'courier') {
            $rules = array(
                'title' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Courier name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'payment_gateway') {
            $rules = array(
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'item_group_master') {
            $rules = array(
                'group_name' => array('rules' => 'required', 'label' => "Group name"),
                'sequence' => array('rules' => 'required', 'label' => "Sequence"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'itemcategory_master') {
            $rules = array(
                'name' => array('rules' => 'required', 'label' => "Category name"),
                'hsn_code' => array('rules' => 'required', 'label' => "HSN code"),
                // 'total_tax' => array('rules' => 'required|integer', 'label' => "Total percentage"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['itemcat_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'item_master') {
            $rules = array(
                'item_name' => array('rules' => 'required', 'label' => "Item name"),
                'category_id' => array('rules' => 'required|integer', 'label' => "Category"),
                'group_id' => array('rules' => 'required|integer', 'label' => "Group"),
                'including_tax' => array('rules' => 'in_list[0,1]', 'label' => "Including tax"),
                'total_rate' => array('rules' => 'required|greater_than[0]', 'label' => "Price including tax"),
                'rate' => array('rules' => 'required|greater_than[0]', 'label' => "Rate"),
                'unit' => array('rules' => 'required', 'label' => "Unit"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['item_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'item_package_master') {
            $rules = array(
                'package_name' => array('rules' => 'required', 'label' => "Package name"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'stockout_inventory') {
            $rules = array(
                'inventory_date' => array('rules' => 'required|isValidDate', 'label' => "Inventory date"),
                'branch_id' => array('rules' => 'required', 'label' => "Branch"),
                'inventory_type_id' => array('rules' => 'required', 'label' => "Inventory Type")
            );
            if ($action == 'update') {
                $rules['inv_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'custom_variables') {
            $rules = array(
                'var_key' => array('rules' => 'required', 'label' => "Key"),
                'var_value' => array('rules' => 'required', 'label' => "Value"),
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'branch_master') {
            if (array_key_exists('form_no', $data)) {
                if ($data['form_no'] == 1) {
                    $rules = array(
                        'branch_name' => array('rules' => 'required', 'label' => "Branch Name"),
                        'branch_code' => array('rules' => 'required', 'label' => "Branch Code"),
                        'country' => array('rules' => 'required', 'label' => "Country"),
                        'state' => array('rules' => 'required', 'label' => "State"),
                        'city' => array('rules' => 'required', 'label' => "City"),
                        'pincode' => array('rules' => 'required', 'label' => "Pincode"),
                        'origin_area' => array('rules' => 'required', 'label' => "Origin Area"),
                        'address' => array('rules' => 'required', 'label' => "Address"),
                    );
                } else if ($data['form_no'] == 2) {
                    $rules = array(
                        'branch_prefix' => array('rules' => 'max_length[50]', 'label' => "Branch Prefix"),
                        'signatory' => array('rules' => 'max_length[100]', 'label' => "Signatory"),
                        'invoice_address' => array('rules' => 'max_length[200]', 'label' => "Address on Invoice"),
                        'invoice_remarks' => array('rules' => 'max_length[2000]', 'label' => "Remarks on Invoice"),
                    );
                } else if ($data['form_no'] == 3) {
                    $rules = array(
                        'sms_url' => array('rules' => 'max_length[250]', 'label' => "SMS API URL"),
                        'whatsapp_url' => array('rules' => 'max_length[250]', 'label' => "WHATSAPP API URL"),
                        'default_message_medium' => array('rules' => 'max_length[50]', 'label' => "Default message medium"),
                    );
                } else if ($data['form_no'] == 4) {
                    $rules = array(
                        'email_host' => array('rules' => 'max_length[250]', 'label' => "Mailer Host"),
                        'email_username' => array('rules' => 'max_length[250]|valid_email', 'label' => "Email username"),
                        'email_password' => array('rules' => 'max_length[50]', 'label' => "Email password"),
                        'email_port' => array('rules' => 'max_length[10]', 'label' => "Email port"),
                        'email_display_name' => array('rules' => 'max_length[100]', 'label' => "Display name"),
                    );
                } else if ($data['form_no'] == 5) {
                    $rules = array(
                        'http_method' => array('rules' => 'max_length[10]|in_list[GET,POST]', 'label' => "HTTP Method"),
                        'dial_api_url' => array('rules' => 'max_length[250]|valid_url', 'label' => "Click To Dial API URL"),
                        'click_2_call_header_json' => array('rules' => 'max_length[200]|valid_json', 'label' => "API Headers"),
                        'dial_api_json' => array('rules' => 'max_length[2000]|valid_json', 'label' => "API Parameters"),
                        'agent_dial_url' => array('rules' => 'max_length[250]|valid_url', 'label' => "Agent - Click To Dial API URL"),
                        'agent_api_parameters' => array('rules' => 'max_length[2000]|valid_json', 'label' => "Agent API Parameters"),
                        'hangup_api_url' => array('rules' => 'max_length[250]|valid_url', 'label' => "Hangup API")
                    );
                }
            }
            if ($action == 'update') {
                $rules['branch_id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'lead_status') {
            $rules = array(
                'title' => array('rules' => 'required', 'label' => "Title"),
                'lead_status_type' => array('rules' => 'required', 'label' => "Lead status type"),
                'color_code' => array('rules' => 'required', 'label' => "Color"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'lead_source') {
            $rules = array(
                'title' => array('rules' => 'required', 'label' => "Title"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'api_provider') {
            $rules = array(
                'title' => array('rules' => 'required', 'label' => "Title"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status")
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
            }
        } elseif ($table_name == 'leads') {
            $rules = array(
                'contact_code' => array('rules' => 'required', 'label' => "Contact code"),
                'contact_no' => array('rules' => 'required|integer', 'label' => "Contact"),
                'client_name' => array('rules' => 'required', 'label' => "Client name"),
                'email' => array('rules' => 'valid_emails', 'label' => "Email"),
                'branch' => array('rules' => 'required|integer', 'label' => "Branch"),
                'lead_date' => array('rules' => 'required|valid_date[Y-m-d]', 'label' => "Lead date"),
                'lead_type' => array('rules' => 'required|integer', 'label' => "Lead type"),
                'lead_status' => array('rules' => 'required|integer', 'label' => "Lead status"),
                'customer_type' => array('rules' => 'required|integer', 'label' => "Customer type"),
                'status' => array('rules' => 'in_list[0,1]', 'label' => "Status"),
                'state' => array('rules' => 'required', 'label' => "State"),
                'pincode' => array('rules' => 'required', 'label' => "Pincode"),
            );
            if ($action == 'update') {
                $rules['id'] = array('rules' => 'required', 'label' => 'Id');
                unset($rules['lead_status']);
            }
        } elseif ($table_name == 'lead_flp_log') {
            $rules = array(
                'flp_lead_id' => array('rules' => 'required', 'label' => "Id"),
                'lead_flp_status' => array('rules' => 'required|integer', 'label' => "Lead status")
            );
            if ($data['lead_flp_status'] ==  1) {
                $rules['lead_flp_date'] = array('rules' => 'required', 'label' => 'FollowUp date');
            }
        } elseif ($table_name == 'order_logic') {
            $rules = array(
                'order_status_id' => array('rules' => 'required', 'label' => "Order Status"),
                'status_action_id' => array('rules' => 'required', 'label' => "Action Order Status")
            );
        } elseif ($table_name == 'purchase_master') {
            $rules = array(
                'branch_id' => array('rules' => 'required', 'label' => "Branch"),
                'vendor_id' => array('rules' => 'required', 'label' => "Vendor"),
                'invoice_number' => array('rules' => 'required', 'label' => "Invoice no."),
                'invoice_date' => array('rules' => 'required|valid_date[Y-m-d]', 'label' => "Invoice date"),
                'gross_amt' => array('rules' => 'required', 'label' => "Total gross amount"),
                'tax_amt' => array('rules' => 'required', 'label' => "Total tax amount"),
                'net_amt' => array('rules' => 'required', 'label' => "Total net amount")
            );
        } elseif ($table_name == 'purchasereturn_master') {
            $rules = array(
                'branch_id' => array('rules' => 'required', 'label' => "Branch"),
                'vendor_id' => array('rules' => 'required', 'label' => "Vendor"),
                'purchase_id' => array('rules' => 'required', 'label' => "Purchase Id"),
                'invoice_date' => array('rules' => 'required|valid_date[Y-m-d]', 'label' => "Invoice date"),
                'gross_amt' => array('rules' => 'required', 'label' => "Total gross amount"),
                'tax_amt' => array('rules' => 'required', 'label' => "Total tax amount"),
                'net_amt' => array('rules' => 'required', 'label' => "Total net amount")
            );
        } elseif ($table_name == 'order_message_template') {
            $rules = array(
                'template_name' => array('rules' => 'required|min_length[2]|max_length[100]', 'label' => "Template Name"),
                'temp_type' => array('rules' => 'required|in_list[SMS,Email,WhatsApp]', 'label' => "Template Type"),
                'template_for'=> array('rules' => 'required|min_length[1]|max_length[300]', 'label' => "Template For"),
            );
            if (\strtolower($data['temp_type']) == 'Email') {
                $rules[] = array('rules' => 'required', 'label' => 'Email body');
                $rules[] = array('rules' => 'subject', 'label' => 'Email Subject');
            }
            if (\strtolower($data['temp_type']) == 'SMS') {
                $rules['template_body'] = array('rules' => 'required', 'label' => 'SMS body');
            }
        } else if($table_name == 'order') {
            $rules = array(
                'phone_code' => array('rules' => 'required|min_length[2]|max_length[10]', 'label' => "Phone Code of Customer"),
                'mobile_number' => array('rules' => 'required|min_length[2]|max_length[10]', 'label' => "Mobile No. of Customer"),
                'address_state_id' => array('rules' => 'required', 'label' => "State"),
                'address_pincode' => array('rules' => 'required', 'label' => "Pincode"),
                'address_city_id' => array('rules' => 'required', 'label' => "City"),
            );
        } else if($table_name == 'order_courier') {
            $rules = array(
                'courier_id'=> array('rules' => 'required|max_length[50]', 'label' => "Courier Id"),
                'order_id'=> array('rules' => 'required|exact_length[6]', 'label' => "Order Id"),
                'courier_expected_del_date'=> array('rules' => 'required|exact_length[10]|valid_date[Y-m-d]', 'label' => "Courier Expected Delivery Date"),
                'courier_product_name' => array('rules' => 'required|max_length[500]', 'label' => "Courier Product Name"),
                'courier_weight' => array('rules' => 'decimal|max_length[10]', 'label' => "Courier Weight"),
                'courier_height' => array('rules' => 'decimal|max_length[10]', 'label' => "Courier Height"),
                'courier_length' => array('rules' => 'decimal|max_length[10]', 'label' => "Courier Length"),
                'courier_breath' => array('rules' => 'decimal|max_length[10]', 'label' => "Courier Breath"),
                'product_type' => array('rules' => 'required|max_length[250]', 'label' => "Product Type"),
                'pickup_date' => array('rules' => 'required|max_length[10]|valid_date[Y-m-d]', 'label' => "Pickup Date"),
                'pickup_hour' => array('rules' => 'required|max_length[50]|is_natural', 'label' => "Pickup Time (Hour)"),
                'pickup_minutes' => array('rules' => 'required|max_length[50]|is_natural', 'label' => "Pickup Time (Minutes)"),
                'customer_code' => array('rules' => 'required|max_length[250]', 'label' => "Customer Code"),
                'origin_area'   => array('rules' => 'required|max_length[250]', 'label' => "Origin Area"),
                'courier_payment_mode' => array('rules' => 'required|max_length[25]', 'label' => "Courier Payment Mode"),
                'courier_ewaybill_no' => array('rules' => 'max_length[50]', 'label' => "Courier Ewaybill Number"),
                'courier_transdistance' => array('rules' => 'max_length[25]', 'label' => "Courier Transdistance"),
                'courier_transporter_id' => array('rules' => 'max_length[250]', 'label' => "Courier Transporter Id"),
                'courier_transporter_name'=> array('rules' => 'max_length[250]', 'label' => "Courier Transporter Name"),
                'courier_type_of_delivery'=> array('rules' => 'max_length[250]', 'label' => "Type Of Delivery"),
                'courier_unit_of_measure' => array('rules' => 'max_length[250]', 'label' => "Unit Of Measure"),
                'courier_type_of_service' => array('rules' => 'max_length[250]', 'label' => "Type Of Service")
            );
        }
        return $rules;
    }
}