<?php
defined('BASEPATH') or exit('No direct script access allowed');

class CustomerSeeder extends BaseSeeder
{
    public function run()
    {
        $companies = [
            'Acme Industries',       'TechCorp Solutions',    'Bright Minds Ltd',     'Skyline Enterprises',
            'Nova Systems',          'Peak Performance Inc',  'Green Valley Foods',   'Prime Logistics',
            'Silverline Trading',    'Golden Gate Retail',    'BlueOcean Marine',     'Sunrise Media',
            'IronPeak Manufacturing','Crystal Bank',          'RedRose Fashion',      'Vertex Consulting',
            'Zenith Analytics',      'Meridian Health',       'CloudNine Services',   'Emerald Estates',
        ];
        $firstNames = ['Amit','Sonia','Rohit','Kavita','Sanjay','Meera','Arun','Deepika','Manoj','Riya','Karan','Pooja','Suresh','Nisha','Vinod','Anita','Rakesh','Sunita','Ashok','Rekha'];
        $lastNames  = ['Sharma','Verma','Patel','Reddy','Kumar','Singh','Iyer','Menon','Nair','Rao','Gupta','Bansal','Malhotra','Kapoor','Chopra','Shah','Desai','Jain','Mehta','Joshi'];
        $cities     = ['Mumbai','Delhi','Bengaluru','Chennai','Kolkata','Pune','Hyderabad','Ahmedabad','Jaipur','Lucknow'];
        $states     = ['Maharashtra','Delhi','Karnataka','Tamil Nadu','West Bengal','Maharashtra','Telangana','Gujarat','Rajasthan','Uttar Pradesh'];

        foreach ($companies as $i => $company) {
            if ($this->exists('clients', ['company' => $company])) {
                $this->log("· Exists: $company");
                continue;
            }

            $fn      = $firstNames[$i % count($firstNames)];
            $ln      = $lastNames[$i % count($lastNames)];
            $city    = $cities[$i % count($cities)];
            $state   = $states[$i % count($states)];
            $daysAgo = rand(0, 45);
            $gst     = '27' . strtoupper(substr(md5($company), 0, 5)) . rand(1000, 9999) . 'F1Z' . rand(0, 9);

            $this->db->insert(db_prefix() . 'clients', [
                'company'          => $company,
                'client_type'      => 'corporate',
                'vat'              => $gst,
                'gst_number'       => $gst,
                'phonenumber'      => '+91' . rand(7000000000, 9999999999),
                'country'          => 101,
                'city'             => $city,
                'state'            => $state,
                'address'          => rand(1, 999) . ', ' . $city . ' Complex',
                'website'          => 'https://' . strtolower(str_replace(' ', '', $company)) . '.com',
                'datecreated'      => date('Y-m-d H:i:s', strtotime("-$daysAgo days")),
                'active'           => 1,
                'default_currency' => 0,
            ]);
            $userid = $this->db->insert_id();

            $this->db->insert(db_prefix() . 'contacts', [
                'userid'        => $userid,
                'is_primary'    => 1,
                'firstname'     => $fn,
                'lastname'      => $ln,
                'email'         => strtolower($fn . '.' . $ln . $i) . '@' . strtolower(str_replace(' ', '', $company)) . '.com',
                'phonenumber'   => '+91' . rand(7000000000, 9999999999),
                'title'         => 'Manager',
                'datecreated'   => date('Y-m-d H:i:s', strtotime("-$daysAgo days")),
                'active'        => 1,
                'pan_number'    => strtoupper(substr(md5($company . $i), 0, 5)) . rand(1000, 9999) . 'A',
                'password'      => app_hash_password('password123'),
                'investor_type' => rand(0, 1) ? 'new' : 'existing',
            ]);
            $this->log("+ Customer: $company ($fn $ln)");
        }
    }
}
