<?php

// 1. Update app/Models/User.php
$content = file_get_contents('app/Models/User.php');
$constants = "
    const ROLE_MEMBER = 0;
    const ROLE_ADMIN = 1;

    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;

    const FLAG_SEND_DISABLED = 0;
    const FLAG_SEND_ENABLED = 1;

    const FIRST_LOGIN_FALSE = 0;
    const FIRST_LOGIN_TRUE = 1;
";
$content = str_replace('use HasFactory, Notifiable;', "use HasFactory, Notifiable;\n" . $constants, $content);
file_put_contents('app/Models/User.php', $content);

// 2. Update app/Models/Administration.php
$content = file_get_contents('app/Models/Administration.php');
$constants = "
    const MAX_SEND_CARD_PER_MONTH = 5;
";
$content = str_replace('use HasFactory;', "use HasFactory;\n" . $constants, $content);
file_put_contents('app/Models/Administration.php', $content);

// 3. Update app/Http/Middleware/AdminMiddleware.php
$content = file_get_contents('app/Http/Middleware/AdminMiddleware.php');
$content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content);
$content = str_replace('Auth::user()->role == 1', 'Auth::user()->role == config('constants.ROLE_ADMIN')', $content);
$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == config('constants.STATUS_ACTIVE')', $content);
file_put_contents('app/Http/Middleware/AdminMiddleware.php', $content);

// 4. Update app/Http/Middleware/MemberMiddleware.php
$content = file_get_contents('app/Http/Middleware/MemberMiddleware.php');
$content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content);
$content = str_replace('Auth::user()->role == 0', 'Auth::user()->role == config('constants.ROLE_MEMBER')', $content);
$content = str_replace('Auth::user()->status == 1', 'Auth::user()->status == config('constants.STATUS_ACTIVE')', $content);
file_put_contents('app/Http/Middleware/MemberMiddleware.php', $content);

// 5. Update app/Http/Middleware/ForceChangePasswordMiddleware.php
$content = file_get_contents('app/Http/Middleware/ForceChangePasswordMiddleware.php');
$content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content);
$content = str_replace('Auth::user()->first_login == 1', 'Auth::user()->first_login == config('constants.FIRST_LOGIN_TRUE')', $content);
file_put_contents('app/Http/Middleware/ForceChangePasswordMiddleware.php', $content);

// 6. Update app/Http/Controllers/AuthController.php
$content = file_get_contents('app/Http/Controllers/AuthController.php');
$content = str_replace('use Illuminate\Support\Facades\Auth;', "use Illuminate\Support\Facades\Auth;\nuse App\Models\User;", $content);
$content = str_replace('$user->status != 1', '$user->status != config('constants.STATUS_ACTIVE')', $content);
$content = str_replace('$user->first_login == 1', '$user->first_login == config('constants.FIRST_LOGIN_TRUE')', $content);
$content = str_replace('$user->role == 1', '$user->role == config('constants.ROLE_ADMIN')', $content);
file_put_contents('app/Http/Controllers/AuthController.php', $content);

// 7. Update app/Http/Controllers/AdminController.php
$content = file_get_contents('app/Http/Controllers/AdminController.php');
$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content);
$content = str_replace("'role' => 'required|in:0,1'", "'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN')", $content);
$content = str_replace("'status' => 1", "'status' => config('constants.STATUS_ACTIVE')", $content);
$content = str_replace("'flag_send' => 0", "'flag_send' => config('constants.FLAG_SEND_DISABLED')", $content);
$content = str_replace("'first_login' => 1", "'first_login' => config('constants.FIRST_LOGIN_TRUE')", $content);
$content = str_replace("\$request->has('flag_send') ? 1 : 0", "\$request->has('flag_send') ? config('constants.FLAG_SEND_ENABLED') : config('constants.FLAG_SEND_DISABLED')", $content);
$content = str_replace("\$user->status = 0", "\$user->status = config('constants.STATUS_INACTIVE')", $content);
file_put_contents('app/Http/Controllers/AdminController.php', $content);

// 8. Update app/Http/Controllers/UserController.php
$content = file_get_contents('app/Http/Controllers/UserController.php');
$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content);
$content = str_replace("max:5'", "max:' . Administration::MAX_SEND_CARD_PER_MONTH", $content);
$content = str_replace("\$sender->flag_send == 0", "\$sender->flag_send == config('constants.FLAG_SEND_DISABLED')", $content);
$content = str_replace("> 5)", "> Administration::MAX_SEND_CARD_PER_MONTH)", $content);
$content = str_replace("tối đa 5 card", 'tối đa " . Administration::MAX_SEND_CARD_PER_MONTH . " card', $content);
$content = str_replace("\$user->first_login = 0", "\$user->first_login = config('constants.FIRST_LOGIN_FALSE')", $content);
$content = str_replace("\$user->role == 1", "\$user->role == config('constants.ROLE_ADMIN')", $content);
file_put_contents('app/Http/Controllers/UserController.php', $content);

// 9. Update app/Console/Commands/ResetCardsCommand.php
$content = file_get_contents('app/Console/Commands/ResetCardsCommand.php');
$content = str_replace("where('status', 1)", "where('status', config('constants.STATUS_ACTIVE'))", $content);
file_put_contents('app/Console/Commands/ResetCardsCommand.php', $content);

// 10. Update resources/views/layouts/app.blade.php
$content = file_get_contents('resources/views/layouts/app.blade.php');
$content = str_replace("Auth::user()->role == 1", "Auth::user()->role == config('constants.ROLE_ADMIN')", $content);
file_put_contents('resources/views/layouts/app.blade.php', $content);

// 11. Update resources/views/admin/users/create.blade.php
$content = file_get_contents('resources/views/admin/users/create.blade.php');
$content = str_replace('value="0"', 'value="{{ config('constants.ROLE_MEMBER') }}"', $content);
$content = str_replace('value="1"', 'value="{{ config('constants.ROLE_ADMIN') }}"', $content);
$content = str_replace("old('role') == '0'", "old('role') == config('constants.ROLE_MEMBER')", $content);
$content = str_replace("old('role') == '1'", "old('role') == config('constants.ROLE_ADMIN')", $content);
file_put_contents('resources/views/admin/users/create.blade.php', $content);

// 12. Update resources/views/admin/users/edit.blade.php
$content = file_get_contents('resources/views/admin/users/edit.blade.php');
$content = str_replace("\$user->role == 1", "\$user->role == config('constants.ROLE_ADMIN')", $content);
file_put_contents('resources/views/admin/users/edit.blade.php', $content);

// 13. Update resources/views/user/change_password.blade.php
$content = file_get_contents('resources/views/user/change_password.blade.php');
$content = str_replace("Auth::user()->first_login == 1", "Auth::user()->first_login == config('constants.FIRST_LOGIN_TRUE')", $content);
file_put_contents('resources/views/user/change_password.blade.php', $content);

echo "Refactored successfully!";
