<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Administration;
use App\Models\AddCard;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class MockDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Clear old data to avoid duplicates when running again
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        User::truncate();
        Administration::truncate();
        AddCard::truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        // The default password is now set to 1234@Dcba according to the design requirements
        $password = md5('1234@Dcba');

        // 1. CREATE USERS
        // Admins
        User::create([
            'msnv' => 9999,
            'name' => 'System Admin',
            'mail' => 'admin@runsystem.net',
            'pass' => $password,
            'departments' => 1,
            'role' => User::ROLE_ADMIN,
            'status' => User::STATUS_ACTIVE,
            'card' => 0,
            'flag_send' => User::FLAG_SEND_ENABLED,
            'first_login' => User::FIRST_LOGIN_FALSE,
        ]);

        User::create([
            'msnv' => 9998,
            'name' => 'Second Admin',
            'mail' => 'admin2@runsystem.net',
            'pass' => $password,
            'departments' => 1,
            'role' => User::ROLE_ADMIN,
            'status' => User::STATUS_ACTIVE,
            'card' => 0,
            'flag_send' => User::FLAG_SEND_ENABLED,
            'first_login' => User::FIRST_LOGIN_FALSE,
        ]);

        // Active Users (Initialize with specific users needed for logs/transactions)
        $users = [
            ['msnv' => 1001, 'name' => 'Nguyễn Văn A', 'mail' => 'nguyenvana@runsystem.net', 'card' => 10, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE],
            ['msnv' => 1002, 'name' => 'Trần Thị Ngọc', 'mail' => 'tranthingoc@runsystem.net', 'card' => 3, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE],
            ['msnv' => 1003, 'name' => 'Lê Kim Thư', 'mail' => 'lekiemthu@runsystem.net', 'card' => 6, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_TRUE], // First-time login
            ['msnv' => 1004, 'name' => 'Phòng Nhân Sự', 'mail' => 'phongnhansu@runsystem.net', 'card' => 0, 'flag' => User::FLAG_SEND_DISABLED, 'first_login' => User::FIRST_LOGIN_FALSE], // Disabled sending permission
            ['msnv' => 1005, 'name' => 'Bản Giang', 'mail' => 'bangiang@runsystem.net', 'card' => 36, 'flag' => User::FLAG_SEND_ENABLED, 'first_login' => User::FIRST_LOGIN_FALSE],
        ];

        // Generate additional users to reach exactly 100 member users
        $firstNames = ['Nguyễn', 'Trần', 'Lê', 'Phạm', 'Hoàng', 'Huỳnh', 'Phan', 'Vũ', 'Võ', 'Đặng'];
        $middleNames = ['Văn', 'Thị', 'Minh', 'Đức', 'Quốc', 'Thanh', 'Hữu', 'Ngọc', 'Quỳnh', 'Thu'];
        $lastNames = ['Anh', 'Bình', 'Chi', 'Dương', 'Em', 'Giang', 'Huy', 'Khánh', 'Lâm', 'Nam', 'Oanh', 'Phúc', 'Quang', 'Sơn', 'Trang', 'Vy'];

        for ($i = 6; $i <= 100; $i++) {
            $randomName = $firstNames[array_rand($firstNames)] . ' ' . $middleNames[array_rand($middleNames)] . ' ' . $lastNames[array_rand($lastNames)];
            $users[] = [
                'msnv' => 1000 + $i,
                'name' => $randomName,
                'mail' => 'dev' . str_pad($i, 3, '0', STR_PAD_LEFT) . '@runsystem.net',
                'card' => rand(0, 30),
                'flag' => rand(0, 5) > 0 ? User::FLAG_SEND_ENABLED : User::FLAG_SEND_DISABLED,
                'first_login' => rand(0, 4) == 0 ? User::FIRST_LOGIN_TRUE : User::FIRST_LOGIN_FALSE
            ];
        }

        foreach ($users as $u) {
            User::create([
                'msnv' => $u['msnv'],
                'name' => $u['name'],
                'mail' => $u['mail'],
                'pass' => $password,
                'departments' => rand(1, 5),
                'role' => User::ROLE_MEMBER,
                'status' => User::STATUS_ACTIVE,
                'card' => $u['card'],
                'flag_send' => $u['flag'],
                'first_login' => $u['first_login'],
            ]);

            // For other users, if they have cards, create a matching purchase log in June 2026
            if (!in_array($u['msnv'], [1001, 1002, 1003, 1005])) {
                if ($u['card'] > 0) {
                    AddCard::create([
                        'buyer' => $u['msnv'],
                        'num_card' => $u['card'],
                        'seller' => 9999,
                        'date' => '2026-06-01'
                    ]);
                }
            }
        }

        // Inactive User
        User::create([
            'msnv' => 9997,
            'name' => 'Nghi Duy',
            'mail' => 'nghiduy@runsystem.net',
            'pass' => $password,
            'departments' => 2,
            'role' => User::ROLE_MEMBER,
            'status' => User::STATUS_INACTIVE,
            'card' => 0,
            'flag_send' => User::FLAG_SEND_DISABLED,
            'first_login' => User::FIRST_LOGIN_FALSE,
        ]);

        // 2. CREATE CARD ADDITION LOGS (add_card table)
        // Manual purchase history for specific users:
        $addCardLogs = [
            // 1001: total 30 cards bought
            ['buyer' => 1001, 'num_card' => 15, 'date' => '2026-06-01'],
            ['buyer' => 1001, 'num_card' => 10, 'date' => '2026-07-01'],
            ['buyer' => 1001, 'num_card' => 5,  'date' => '2026-08-01'],

            // 1002: total 15 cards bought
            ['buyer' => 1002, 'num_card' => 10, 'date' => '2026-06-02'],
            ['buyer' => 1002, 'num_card' => 5,  'date' => '2026-07-01'],

            // 1003: total 15 cards bought
            ['buyer' => 1003, 'num_card' => 10, 'date' => '2026-06-03'],
            ['buyer' => 1003, 'num_card' => 5,  'date' => '2026-08-02'],

            // 1005: total 50 cards bought
            ['buyer' => 1005, 'num_card' => 50, 'date' => '2026-06-04'],
        ];

        foreach ($addCardLogs as $log) {
            AddCard::create([
                'buyer' => $log['buyer'],
                'num_card' => $log['num_card'],
                'seller' => 9999,
                'date' => $log['date']
            ]);
        }

        // 3. CREATE CARD SENDING HISTORY (administration table)
        $transactions = [
            // June 2026 transactions
            ['sender' => 1001, 'receiver' => 1005, 'amount' => 5, 'date' => '2026-06-05'],
            ['sender' => 1001, 'receiver' => 1002, 'amount' => 3, 'date' => '2026-06-12'],
            ['sender' => 1002, 'receiver' => 1004, 'amount' => 4, 'date' => '2026-06-10'],
            ['sender' => 1003, 'receiver' => 1001, 'amount' => 2, 'date' => '2026-06-15'],
            ['sender' => 1005, 'receiver' => 1002, 'amount' => 5, 'date' => '2026-06-20'],

            // July 2026 transactions
            ['sender' => 1001, 'receiver' => 1002, 'amount' => 5, 'date' => '2026-07-02'],
            ['sender' => 1001, 'receiver' => 1003, 'amount' => 3, 'date' => '2026-07-10'],
            ['sender' => 1002, 'receiver' => 1001, 'amount' => 5, 'date' => '2026-07-15'],
            ['sender' => 1003, 'receiver' => 1004, 'amount' => 4, 'date' => '2026-07-08'],
            ['sender' => 1005, 'receiver' => 1002, 'amount' => 4, 'date' => '2026-07-12'],

            // August 2026 transactions
            ['sender' => 1001, 'receiver' => 1005, 'amount' => 4, 'date' => '2026-08-05'],
            ['sender' => 1002, 'receiver' => 1003, 'amount' => 3, 'date' => '2026-08-12'],
            ['sender' => 1003, 'receiver' => 1001, 'amount' => 3, 'date' => '2026-08-10'],
            ['sender' => 1005, 'receiver' => 1004, 'amount' => 5, 'date' => '2026-08-15'],
        ];

        foreach ($transactions as $t) {
            // Sender record
            Administration::create([
                'msnv' => $t['sender'],
                'received' => 0,
                'sender' => null,
                'sent' => $t['amount'],
                'receiver' => $t['receiver'],
                'date' => $t['date']
            ]);

            // Receiver record
            Administration::create([
                'msnv' => $t['receiver'],
                'received' => $t['amount'],
                'sender' => $t['sender'],
                'sent' => 0,
                'receiver' => null,
                'date' => $t['date']
            ]);
        }
    }
}
