<?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');
        $depts = config('constants.DEPARTMENTS');

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

        User::create([
            'msnv' => 9998,
            'name' => 'Second Admin',
            'mail' => 'admin2@runsystem.net',
            'pass' => $password,
            'departments' => $depts[0],
            'role' => config('constants.ROLE_ADMIN'),
            'status' => config('constants.STATUS_ACTIVE'),
            'card' => 0,
            'flag_send' => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.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' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')],
            ['msnv' => 1002, 'name' => 'Trần Thị Ngọc', 'mail' => 'tranthingoc@runsystem.net', 'card' => 3, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')],
            ['msnv' => 1003, 'name' => 'Lê Kim Thư', 'mail' => 'lekiemthu@runsystem.net', 'card' => 6, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_TRUE')], // First-time login
            ['msnv' => 1004, 'name' => 'Phòng Nhân Sự', 'mail' => 'phongnhansu@runsystem.net', 'card' => 0, 'flag' => config('constants.FLAG_SEND_DISABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')], // Disabled sending permission
            ['msnv' => 1005, 'name' => 'Bản Giang', 'mail' => 'bangiang@runsystem.net', 'card' => 36, 'flag' => config('constants.FLAG_SEND_ENABLED'), 'first_login' => config('constants.FIRST_LOGIN_FALSE')],
        ];

        foreach ($users as $idx => $u) {
            User::create([
                'msnv' => $u['msnv'],
                'name' => $u['name'],
                'mail' => $u['mail'],
                'pass' => $password,
                'departments' => $depts[$idx % count($depts)],
                'role' => config('constants.ROLE_MEMBER'),
                'status' => config('constants.STATUS_ACTIVE'),
                'card' => $u['card'],
                'flag_send' => $u['flag'],
                'first_login' => $u['first_login'],
            ]);
        }

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

        // 2. CREATE MOCK TRANSACTIONS
        // Nguyễn Văn A (1001) receives 3 cards from Trần Thị Ngọc (1002)
        Administration::create([
            'msnv'     => '1001',
            'received' => 3,
            'sender'   => '1002',
            'sent'     => 0,
            'receiver' => null,
            'date'     => Carbon::now()->format('Y-m-d'),
        ]);
        Administration::create([
            'msnv'     => '1002',
            'received' => 0,
            'sender'   => null,
            'sent'     => 3,
            'receiver' => '1001',
            'date'     => Carbon::now()->format('Y-m-d'),
        ]);

        // Nguyễn Văn A (1001) receives 2 cards from Bản Giang (1005)
        Administration::create([
            'msnv'     => '1001',
            'received' => 2,
            'sender'   => '1005',
            'sent'     => 0,
            'receiver' => null,
            'date'     => Carbon::now()->subDays(2)->format('Y-m-d'),
        ]);
        Administration::create([
            'msnv'     => '1005',
            'received' => 0,
            'sender'   => null,
            'sent'     => 2,
            'receiver' => '1001',
            'date'     => Carbon::now()->subDays(2)->format('Y-m-d'),
        ]);

        // Nguyễn Văn A (1001) sends 4 cards to Trần Thị Ngọc (1002)
        Administration::create([
            'msnv'     => '1001',
            'sent'     => 4,
            'receiver' => '1002',
            'received' => 0,
            'sender'   => null,
            'date'     => Carbon::now()->subDays(1)->format('Y-m-d'),
        ]);
        Administration::create([
            'msnv'     => '1002',
            'sent'     => 0,
            'receiver' => null,
            'received' => 4,
            'sender'   => '1001',
            'date'     => Carbon::now()->subDays(1)->format('Y-m-d'),
        ]);
    }
}
