# BookDash - Command Reference

## 🚀 Initial Setup Commands

```bash
# 1. Copy environment file
cp .env.example .env

# 2. Install PHP dependencies
composer install

# 3. Install Node dependencies
npm install

# 4. Generate application key
php artisan key:generate

# 5. Create database (run in MySQL)
mysql -u root -p
CREATE DATABASE bookdash;
exit;

# 6. Run migrations
php artisan migrate

# 7. Create storage link
php artisan storage:link

# 8. Seed admin user
php artisan db:seed --class=AdminSeeder

# 9. Build frontend assets
npm run build
# OR for development with hot reload
npm run dev

# 10. Start development server
php artisan serve
```

## 📦 Laravel Breeze Installation (Authentication)

```bash
# Install Breeze
composer require laravel/breeze --dev

# Install Blade stack
php artisan breeze:install blade

# Install dependencies and build
npm install
npm run build

# Run migrations (for auth tables)
php artisan migrate
```

## 🗄️ Database Commands

```bash
# Run migrations
php artisan migrate

# Rollback last migration
php artisan migrate:rollback

# Rollback all migrations
php artisan migrate:reset

# Fresh migration (WARNING: Deletes all data!)
php artisan migrate:fresh

# Fresh migration with seeders
php artisan migrate:fresh --seed

# Create a new migration
php artisan make:migration create_tablename_table

# Create a new seeder
php artisan make:seeder TableSeeder

# Run specific seeder
php artisan db:seed --class=AdminSeeder

# Run all seeders
php artisan db:seed
```

## 🏗️ Generate Commands

```bash
# Create controller
php artisan make:controller ControllerName

# Create model
php artisan make:model ModelName

# Create model with migration
php artisan make:model ModelName -m

# Create model with migration, factory, and seeder
php artisan make:model ModelName -mfs

# Create middleware
php artisan make:middleware MiddlewareName

# Create policy
php artisan make:policy PolicyName --model=ModelName

# Create notification
php artisan make:notification NotificationName

# Create job
php artisan make:job JobName

# Create form request
php artisan make:request RequestName

# Create test
php artisan make:test TestName
```

## 🧹 Cache & Optimization Commands

```bash
# Clear all caches
php artisan optimize:clear

# Or individually:
php artisan cache:clear       # Application cache
php artisan config:clear      # Configuration cache
php artisan route:clear       # Route cache
php artisan view:clear        # View cache
php artisan event:clear       # Event cache

# Cache for production
php artisan config:cache      # Cache config
php artisan route:cache       # Cache routes
php artisan view:cache        # Cache views
php artisan event:cache       # Cache events

# Optimize autoloader
composer dump-autoload -o
```

## 🔄 Queue Commands

```bash
# Create jobs table
php artisan queue:table
php artisan migrate

# Start queue worker
php artisan queue:work

# Start queue worker for specific queue
php artisan queue:work --queue=high,default

# Start queue worker with timeout
php artisan queue:work --timeout=90

# Process only one job
php artisan queue:work --once

# Restart all queue workers
php artisan queue:restart

# Check failed jobs
php artisan queue:failed

# Retry failed job
php artisan queue:retry {job_id}

# Retry all failed jobs
php artisan queue:retry all

# Clear failed jobs
php artisan queue:flush
```

## 🛠️ Tinker (Interactive Shell)

```bash
# Start Tinker
php artisan tinker

# Once in Tinker:
>>> $user = new App\Models\User();
>>> $user->name = 'John Doe';
>>> $user->email = 'john@example.com';
>>> $user->password = bcrypt('password');
>>> $user->role = 'user';
>>> $user->credit_balance = 10;
>>> $user->save();

>>> // Find users
>>> App\Models\User::all();
>>> App\Models\User::find(1);
>>> App\Models\User::where('email', 'admin@bookdash.app')->first();

>>> // Create book
>>> $book = App\Models\Book::create([
...   'user_id' => 1,
...   'title' => 'My Test Book',
...   'description' => 'Testing',
...   'status' => 'draft'
... ]);

>>> // Exit Tinker
>>> exit
```

## 📊 BookDash Specific Commands

### Create Admin User (via Tinker)
```bash
php artisan tinker

>>> $user = App\Models\User::create([
...   'name' => 'Admin',
...   'email' => 'admin@bookdash.app',
...   'password' => bcrypt('password'),
...   'role' => 'admin',
...   'credit_balance' => 100,
...   'email_verified_at' => now()
... ]);
>>> $user->generateUniqueEmail();
>>> exit
```

### Add Credits to User
```bash
php artisan tinker

>>> $user = App\Models\User::find(1);
>>> $user->addCredit(50, 'admin_adjustment', 'Added by admin');
>>> exit
```

### Check User Books
```bash
php artisan tinker

>>> $user = App\Models\User::find(1);
>>> $user->books;
>>> $user->books()->count();
>>> exit
```

### View Credit Transactions
```bash
php artisan tinker

>>> App\Models\Credit::latest()->take(10)->get();
>>> exit
```

## 🧪 Testing Commands

```bash
# Run all tests
php artisan test

# Run specific test
php artisan test --filter TestName

# Run tests with coverage
php artisan test --coverage

# Create test
php artisan make:test BookTest
php artisan make:test BookTest --unit
```

## 🔐 Permission Commands (Linux/Mac)

```bash
# Fix storage permissions
chmod -R 775 storage bootstrap/cache

# Fix ownership (replace 'www-data' with your web server user)
chown -R www-data:www-data storage bootstrap/cache
```

## 📦 Composer Commands

```bash
# Install dependencies
composer install

# Update dependencies
composer update

# Install specific package
composer require package/name

# Install dev dependency
composer require --dev package/name

# Remove package
composer remove package/name

# Dump autoload
composer dump-autoload
```

## 📦 NPM Commands

```bash
# Install dependencies
npm install

# Build for production
npm run build

# Development build with watch
npm run dev

# Update packages
npm update

# Install specific package
npm install package-name

# Remove package
npm remove package-name
```

## 🚢 Deployment Commands

```bash
# Optimize for production
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache
npm run build

# Set permissions
chmod -R 775 storage bootstrap/cache

# Create symbolic link
php artisan storage:link

# Run migrations
php artisan migrate --force
```

## 🔍 Debugging Commands

```bash
# Show routes
php artisan route:list

# Show routes for specific URI
php artisan route:list --path=books

# Show all registered events
php artisan event:list

# Show all registered commands
php artisan list

# Show Laravel version
php artisan --version

# Show environment info
php artisan about

# Enable maintenance mode
php artisan down

# Disable maintenance mode
php artisan up

# Tail log file
tail -f storage/logs/laravel.log
```

## 📧 Email Testing (Mailhog/Mailtrap)

```bash
# If using Mailhog locally
# Update .env:
MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025

# Send test notification
php artisan tinker
>>> $user = App\Models\User::find(1);
>>> $user->notify(new App\Notifications\LowCreditWarning(5));
```

## 🔄 Common Development Workflow

```bash
# 1. Start development environment
php artisan serve

# 2. In another terminal, start Vite
npm run dev

# 3. In another terminal, start queue worker
php artisan queue:work

# 4. Watch logs
tail -f storage/logs/laravel.log

# When making changes:
php artisan route:clear
php artisan config:clear
php artisan cache:clear
```

## ⚡ Quick Reset (Development Only!)

```bash
# WARNING: This deletes all data!
php artisan migrate:fresh --seed
php artisan optimize:clear
npm run build
```

## 📱 Mobile/CORS Setup

```bash
# If building API
composer require fruitcake/laravel-cors

# Publish config
php artisan vendor:publish --provider="Fruitcake\Cors\CorsServiceProvider"
```

## 🎨 Asset Management

```bash
# Compile assets
npm run build

# Watch for changes
npm run dev

# Clear compiled assets
rm -rf public/build

# Regenerate manifest
npm run build
```

## 💾 Backup Commands

```bash
# Backup database
php artisan backup:run

# Install Laravel Backup package first:
composer require spatie/laravel-backup

# Publish config
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
```

## 🔧 Troubleshooting Commands

```bash
# Class not found? Rebuild autoload
composer dump-autoload

# Routes not working? Clear cache
php artisan route:clear
php artisan config:clear

# Views not updating? Clear view cache
php artisan view:clear

# Permission denied? Fix permissions
chmod -R 775 storage bootstrap/cache

# Database connection error? Check .env and run
php artisan config:clear

# 500 error? Check logs
cat storage/logs/laravel.log
```

## 🎯 BookDash Workflow Commands

### Starting Fresh
```bash
php artisan migrate:fresh
php artisan db:seed --class=AdminSeeder
php artisan serve
```

### Adding Test Data
```bash
php artisan tinker
>>> App\Models\User::factory()->count(10)->create();
>>> App\Models\Book::factory()->count(20)->create();
```

### Checking System Status
```bash
php artisan route:list --path=books
php artisan queue:work --once
php artisan about
```

---

**Pro Tip:** Create aliases in your shell for commonly used commands:
```bash
alias art="php artisan"
alias mfs="php artisan migrate:fresh --seed"
alias pserve="php artisan serve"
alias tinker="php artisan tinker"
```

Then you can use:
```bash
art migrate
mfs
pserve
tinker
```
