<?php
namespace App\Config;
use App\Config\Dropdown;

class Main
{
    use Dropdown;

    public static $client_db_prefix =  'sanordo_clientdb_';

    private static $admin_tables = [
        'admin_user_master'              => 'admin_user_master',
        'admin_users_rights'             => 'admin_users_rights',
        'admin_menu_master'              => 'admin_menu_master',
        'admin_template'                 => 'admin_template',
        'admin_template_assign'          => 'admin_template_assign',
        'admin_settings'                 => 'admin_settings',
        'admin_plan_master'              => 'admin_plan_master',
        'admin_client_master'            => 'admin_client_master',
        'admin_client_plan_log'          => 'admin_client_plan_log',
        'admin_client_payment_log'       => 'admin_client_payment_log',
        'admin_user_login_log'           => 'admin_user_login_log',
        'admin_email_sms_logs'           => 'admin_email_sms_logs',
        'admin_country_master'           => 'admin_country_master',
        'admin_state_master'             => 'admin_state_master',
        'lead_status_type'               => 'lead_status_type',
        'admin_courier_api_master'       => 'admin_courier_api_master',
        'admin_payment_gateway_master'   => 'admin_payment_gateway_master',
        'admin_lead_source_api_provider' => 'admin_lead_source_api_provider',
        'whatsapp_solution_providers'    => 'whatsapp_solution_providers',
        'dial_solution_providers'        => 'dial_solution_providers',
        'sms_solution_providers'         => 'sms_solution_providers'
    ];

    private static $client_tables = [
        'client_user_login_log'       => 'client_user_login_log',
        'client_users_master'         => 'client_users_master',
        'users_profile_rights'        => 'users_profile_rights',
        'users_group_master'          => 'users_group_master',
        'client_menu_master'          => 'client_menu_master',
        'disposition_master'          => 'disposition_master',
        'tax_master'                  => 'tax_master',
        'coupon_master'               => 'coupon_master',
        'coupon_list'                 => 'coupon_list',
        'coupon_used'                 => 'coupon_used',
        'location_state'              => 'location_state',
        'location_city'               => 'location_city',
        'location_pincode'            => 'location_pincode',
        'customer_category_master'    => 'customer_category_master',
        'customer_master'             => 'customer_master',
        'lead_type'                   => 'lead_type',
        'customer_address_master'     => 'customer_address_master',
        'order_status'                => 'order_status',
        'campaign_master'             => 'campaign_master',
        'payment_type'                => 'payment_type',
        'inventory_type'              => 'inventory_type',
        'invoice_type'                => 'invoice_type',
        'courier'                     => 'courier',
        'payment_gateway'             => 'payment_gateway',
        'item_group_master'           => 'item_group_master',
        'itemcategory_master'         => 'itemcategory_master',
        'item_master'                 => 'item_master',
        'item_package_master'         => 'item_package_master',
        'item_package_item_mapping'   => 'item_package_item_mapping',
        'item_stock'                  => 'item_stock',
        'stockout_inventory'          => 'stockout_inventory',
        'stockout_inventory_item'     => 'stockout_inventory_item',
        'branch_master'               => 'branch_master',
        'branch_user_rights'          => 'branch_user_rights',
        'custom_variables'            => 'custom_variables',
        'lead_status'                 => 'lead_status',
        'lead_source'                 => 'lead_source',
        'api_provider'                => 'api_provider',
        'leads'                       => 'leads',
        'lead_item_list'              => 'lead_item_list',
        'lead_tax_list'               => 'lead_tax_list',
        'lead_flp_log'                => 'lead_flp_log',
        'order_logic'                 => 'order_logic',
        'email_sms_log'               => 'email_sms_log',
        'api_user_logs'               => 'api_user_logs',
        'purchase_master'             => 'purchase_master',
        'purchasereturn_master'       => 'purchasereturn_master',
        'itemcategorytax_master'      => 'itemcategorytax_master',
        'order'                       => 'order_master',
        'order_item_list'             => 'order_item_list',
        'order_tax_list'              => 'order_taxlist',
        'order_logs'                  => 'order_logs',
        'lead_order_logs'             => 'lead_order_logs',
        'courier_docketno'            => 'courier_docketno',
        'courier_pincode'             => 'courier_pincode',
        'client_template_for'         => 'client_template_for',
        'order_message_template'      => 'order_message_template',
        'client_template_variables'   => 'client_template_variables',
        'order_courier'               => 'order_courier',
        'order_assign_logs'           => 'order_assign_logs',
        'order_shopify_log'           => 'order_shopify_log',
        'order_shopify_process_log'   => 'order_shopify_process_log',  
        'order_status_update_logs'    => 'order_status_update_logs',
        'order_payment_link_logs'     => 'order_payment_link_logs',
        'payment_webhook_logs'        => 'payment_webhook_logs',
        'order_status_update_webhook_log'   => 'order_status_update_webhook_log',
        'order_status_update_logs'    => 'order_status_update_logs',
        'order_attachments'           => 'order_attachments',
        'users_master'                => 'users_master'
    ];

    public static $mastersConfig = [
        'client_users_master'       => ['primaryKey' => 'user_id', 'clientDB' => false],
        'disposition_master'        => ['primaryKey' => 'disposition_id', 'clientDB' => true],
        'tax_master'                => ['primaryKey' => 'tax_id', 'clientDB' => true],
        'location_city'             => ['primaryKey' => 'id', 'clientDB' => true],
        'location_pincode'          => ['primaryKey' => 'id', 'clientDB' => true],
        'customer_category_master'  => ['primaryKey' => 'id', 'clientDB' => true],
        'customer_master'           => ['primaryKey' => 'id', 'clientDB' => true],
        'lead_type'                 => ['primaryKey' => 'id', 'clientDB' => true],
        'order_status'              => ['primaryKey' => 'id', 'clientDB' => true],
        'coupon_master'             => ['primaryKey' => 'id', 'clientDB' => true],
        'coupon_used'               => ['primaryKey' => 'id', 'clientDB' => true],
        'users_group_master'        => ['primaryKey' => 'id', 'clientDB' => true],
        'campaign_master'           => ['primaryKey' => 'campaign_id', 'clientDB' => true],
        'payment_type'              => ['primaryKey' => 'id', 'clientDB' => true],
        'inventory_type'            => ['primaryKey' => 'id', 'clientDB' => true],
        'invoice_type'              => ['primaryKey' => 'id', 'clientDB' => true],
        'courier'                   => ['primaryKey' => 'id', 'clientDB' => true],
        'item_group_master'         => ['primaryKey' => 'id', 'clientDB' => true],
        'itemcategory_master'       => ['primaryKey' => 'itemcat_id', 'clientDB' => true],
        'item_master'               => ['primaryKey' => 'item_id', 'clientDB' => true],
        'item_package_master'       => ['primaryKey' => 'id', 'clientDB' => true],
        'stockout_inventory'        => ['primaryKey' => 'inv_id', 'clientDB' => true],
        'payment_gateway'           => ['primaryKey' => 'id', 'clientDB' => true],
        'item_stock'                => ['primaryKey' => 'stock_id', 'clientDB' => true],
        'branch_master'             => ['primaryKey' => 'branch_id', 'clientDB' => true],
        'custom_variables'          => ['primaryKey' => 'id', 'clientDB' => true],
        'lead_status'               => ['primaryKey' => 'id', 'clientDB' => true],
        'lead_source'               => ['primaryKey' => 'id', 'clientDB' => true],
        'api_provider'              => ['primaryKey' => 'id', 'clientDB' => true],
        'leads'                     => ['primaryKey' => 'id', 'clientDB' => true],
        'lead_flp_log'              => ['primaryKey' => 'id', 'clientDB' => true],
        'order_logic'               => ['primaryKey' => 'order_status_id', 'clientDB' => true],
        'email_sms_log'             => ['primaryKey' => 'id', 'clientDB' => true],
        'client_user_login_log'     => ['primaryKey' => 'user_id', 'clientDB' => false],
        'api_user_logs'             => ['primaryKey' => 'id', 'clientDB' => true],
        'purchase_master'           => ['primaryKey' => 'id', 'clientDB' => true],
        'purchasereturn_master'     => ['primaryKey' => 'id', 'clientDB' => true],
        'order'                     => ['primaryKey' => 'id', 'clientDB' => true],
        'lead_order_logs'           => ['primaryKey' => 'id', 'clientDB' => true],
        'admin_payment_gateway_master' => ['primaryKey' => 'id', 'clientDB' => false],
        'admin_courier_api_master'     => ['primaryKey' => 'id', 'clientDB' => false],
        'courier_docketno'             => ['primaryKey' => 'id', 'clientDB' => true],
        'courier_pincode'              => ['primaryKey' => 'id', 'clientDB' => true],
        'client_template_for'          => ['primaryKey' => 'id', 'clientDB' => false],
        'order_message_template'       =>  ['primaryKey' => 'id', 'clientDB' => true],
        'client_template_variables'    =>  ['primaryKey' => 'id', 'clientDB' => false],
        'order_status_update_logs'     =>  ['primaryKey' => 'id', 'clientDB' => true],
        'order_attachments'            =>  ['primaryKey' => 'id', 'clientDB' => true],
        'users_master'                 =>  ['primaryKey' => 'user_id', 'clientDB' => true],
    ];

    public static $api_variables = array(
        'agent_extention' => '{{ext_no}}',
        'agent_mobile_no' => '{{agent_mobile_no}}',
        'order_id' => '{{order_id}}',
        'lead_id' => '{{lead_id}}',
        'agent_id' => '{{agent_id}}',
        'campaign_name' => '{{campaign_name}}',
        'customer_mobile_no' => '{{customer_mobile_no}}',
        'uui' => '{{uui}}',
        'message'=> '{{message}}',
        'dlt_template_id'=> '{{dlt_template_id}}',
        'sms_sender_id'=> '{{sms_sender_id}}'
    );

    public static $whatsapp_variables = array(
        'recipient_no' => '{#recipient_no#}',
        'message' => '{#message#}',
        'doc_type' => '{#doc_type#}',
        'doc_name' => '{#doc_name#}',
        'doc_link' => '{#doc_link#}',
        'curl_file'=> '{#curl_file#}',
    );

    public static $import_permission = array(
        array("id" => 1, "text" => 'Upload User'),
        array("id" => 2, "text" => 'Upload Lead'),
        array("id" => 3, "text" => 'Upload Customer'),
        array("id" => 4, "text" => 'Upload Orders')
    );

    public static $leadSourceAPIMethod = array(
        array("id" => 'GET', "text" => 'GET'),
        array("id" => 'POST', "text" => 'POST'),
    );

    public static $OrderStatusType = [
        array("value" => 1,  "title" => 'Invoice'),
        array("value" => 21, "title" => 'Invoice Return'),
        array("value" => 2,  "title" => 'Dispatch'),
        array("value" => 3,  "title" => 'Payment'),
        array("value" => 5,  "title" => 'Hold'),
        array("value" => 6,  "title" => 'Item Verify'),
        array("value" => 7,  "title" => 'Cancelled'),
        array("value" => 8,  "title" => 'RTO'),
        array("value" => 9,  "title" => 'Multi Action', 'info' => "Afer Marking this a button will be visible to change status for multiple order at once, You will find the button in order list"),
        array("value" => 10, "title" => 'WhatsApp Confirm'),
        array("value" => 15, "title" => 'WhatsApp Cancelled'),
        array("value" => 11, "title" => 'Manual Confirm'),
        array("value" => 16, "title" => 'Field Pickup Done'),
        array("value" => 17, "title" => 'In Transit'),
        array("value" => 18, "title" => 'Out For Delivery'),
        array("value" => 19, "title" => 'Delivered'),
        array("value" => 20, "title" => 'Undelivered'),
        array("value" => 12, "title" => 'Move to Shopify'),
        array("value" => 14, "title" => 'Shopify Order Fulfilled'),
        array("value" => 13, "title" => 'Shopify Order Not Fulfilled')
    ];

    public static function dbConfig(): array
    {
        return [
            "db_host" => !empty($_ENV['DB_HOST']) ? $_ENV['DB_HOST'] : "",
            "db_port" => !empty($_ENV['DB_PORT']) ? $_ENV['DB_PORT'] :  3306,
            "db_user" => !empty($_ENV['DB_USER']) ? $_ENV['DB_USER'] : "",
            "db_password" => !empty($_ENV['DB_PASS']) ? $_ENV['DB_PASS'] : "",
            "db_name" => !empty($_ENV['DB_NAME']) ? $_ENV['DB_NAME'] : ""
        ];
    }

    public static function dbTables(): array
    {
        return array_merge(self::$admin_tables, self::$client_tables);
    }

    public static function dbProcedures(): array
    {
        return [
            'GetClientMenus'        => 'GetClientMenus',
            'GetLeadDashboardCards' => 'lead_dashboard_cards',
            'GetLeadStatusPiechart' => 'lead_status_piechart',
            'GetLeadSourcePiechart' => 'lead_source_piechart',
            'GetLeadMonthWise'      => 'lead_dash_monthwise',
            'GetLeadUserWise'       => 'lead_dash_userwise',
            'GetOrderList'          => 'GetOrderList',
            'GetOrderDetail'        => 'GetOrderDetail',
            'GetOrderItemList'      => 'GetOrderItemList',
            'GetOrderTaxList'       => 'GetOrderTaxList',
            'GetOrderLogList'       => 'GetOrderLogList',
            'GetMaxInvoiceSeries'   => 'GetMaxInvoiceSeries',
            'GetUserDetail'         => 'GetUserDetail',
            'GetPackageItemList'    => 'GetPackageItemList',
            'GetCustomerAddress'    => 'GetCustomerAddress',
            'GetCourierDetail'      => 'GetCourierDetail',
            'GetCouponDetail'       => 'GetCouponDetail',
            'GetOrderDetailForCourier'  => 'GetOrderDetailForCourier',
            'GetOrderListForReport' => 'GetOrderListForReport',
            'GetGSTReport'          => 'GetGSTReport',
            'GetGSTReportTaxPerWise'    => 'GetGSTReportTaxPerWise',
            'GetGSTReportStateWise' => 'GetGSTReportStateWise',
            'GetGSTReportRegisteredWise'=> 'GetGSTReportRegisteredWise',
            'GetGSTReportUnRegisteredWise'=> 'GetGSTReportUnRegisteredWise',
            'GetGSTReportItemWise'  => 'GetGSTReportItemWise',
            'GetGSTReportHSNWise'   => 'GetGSTReportHSNWise',
            'GetGSTReportHSNSummaryWise'  => 'GetGSTReportHSNSummaryWise',
            'GetInventoryList'      => 'GetInventoryList',
            'GetChildUsers'         => 'GetChildUsers',
            'GetBranchDetail'       => 'GetBranchDetail',
            'getDashboardReportList'  => 'getDashboardReportList',
            'getoverallReportList'  => 'getoverallReportList',
            'getShipmentStatusReport'   => 'getShipmentStatusReport',
            'GetClientUserDetail'   => 'GetClientUserDetail'

        ];
    }
    
    public static function KafkaTopics() :array {
        return [
            "sanordo_update_order_status",
            "sanordo_save_servicable_pincodes",
            "sanordo_save_docketno",
            "sanordo_share_payment_link",
            "sanordo_move_order_shopify",
            "sanordo_send_admin_notifications",
            "sanordo_create_order_from_lead",
            "sanordo_client_email_sms_whatsapp"
        ];
    }

    public static function WebhookURLs() {
        $baseurl = self::getPublicURL();
        return [
            "shopify" => $baseurl.'/webhook/shopify',
            "razorpay" => $baseurl.'/webhook/razorpay',
            "shiprocket" => $baseurl.'/webhook/courier'
        ];
    }

    public static function getLogPath(): string
    {
        return !empty($_ENV['LOG_PATH']) ? $_ENV['LOG_PATH'] : realpath(__DIR__ . '/../../../') . '/logs';
    }

    public static function getUploadFolder(): string
    {
        return !empty($_ENV['ATTACHMENT_DIR']) ? $_ENV['ATTACHMENT_DIR'] : 'uploads';
    }
    public static function getBasePath(): string
    {
        return $_ENV['SERVER_PATH'];
    }
    public static function getUploadPath(): string
    {
        return !empty($_ENV['SERVER_PATH']) && !empty($_ENV['ATTACHMENT_DIR']) ? $_ENV['SERVER_PATH'] . '/' . $_ENV['ATTACHMENT_DIR'] : realpath(__DIR__ . '/../../../') . '/uploads';
    }

    public static function getLogoUploadMaxSize(): int
    {
        return !empty($_ENV['LOGO_UPLOAD_MAX_SIZE_BYTES']) ? (int)$_ENV['LOGO_UPLOAD_MAX_SIZE_BYTES'] : 1048576;
    }

        public static function getUploadMaxSize(): int
    {
        return !empty($_ENV['UPLOAD_MAX_SIZE_BYTES']) ? (int)$_ENV['UPLOAD_MAX_SIZE_BYTES'] : 2097152;
    }
    
    public static function getAllowedLogoFileTypes(): array
    {
        return !empty($_ENV['ALLOWED_LOGO_FILE_TYPES']) ? explode(',', $_ENV['ALLOWED_LOGO_FILE_TYPES']) : ['image/jpg', 'image/jpeg', 'image/png'];
    }

    public static function getKafkaBootstrapServer(): string
    {
        return !empty($_ENV['KAFKA_BOOTSTRAP_SERVER']) ? $_ENV['KAFKA_BOOTSTRAP_SERVER'] : 'localhost:9092';
    }

    public static function getKafkaTopic(): string
    {
        return !empty($_ENV['KAFKA_TOPIC']) ? $_ENV['KAFKA_TOPIC'] : 'sanordo';
    }

    public static function getCouponDiscountType(): int 
    {
        return !empty($_ENV['COUPON_DISCOUNT_TYPE']) ? $_ENV['COUPON_DISCOUNT_TYPE'] : 0;
    }

    public static function getShopifyApiVersion(): string 
    {
        return !empty($_ENV['SHOPIFY_API_VERSION']) ? $_ENV['SHOPIFY_API_VERSION'] : '2025-10';
    }

    public static function getPublicURL(): string 
    {
        return !empty($_ENV['PUBLIC_URL']) ? $_ENV['PUBLIC_URL'] : '';
    }

    public static function getKafkaBroker():string {
        return !empty($_ENV['KAFKA_BOOTSTRAP_SERVER']) ? $_ENV['KAFKA_BOOTSTRAP_SERVER'] : '';
    }

    public static function getNodeServer():string {
        return !empty($_ENV['NODE_SERVER_URL']) ? $_ENV['NODE_SERVER_URL'] : '';
    }
}
