# Agency PM — Setup Guide

## Prerequisites
- PHP 8.2+
- Composer
- Node.js 18+
- MySQL 8 or PostgreSQL 14+

---

## Backend (Laravel)

### 1. Install Laravel
```bash
cd backend

# If starting fresh, scaffold Laravel first:
composer create-project laravel/laravel . --prefer-dist

# Then copy the files from this repo into the scaffolded project
```

### 2. Install dependencies
```bash
composer install
composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
```

### 3. Configure environment
```bash
cp .env.example .env
php artisan key:generate
```

Edit `.env` with your database credentials:
```
DB_DATABASE=agency_pm
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password
```

### 4. Create database & run migrations
```bash
mysql -u root -p -e "CREATE DATABASE agency_pm;"
php artisan migrate
```

### 5. Create the first admin user
```bash
php artisan tinker
```
```php
App\Models\User::create([
    'name'     => 'Admin',
    'email'    => 'admin@agency.com',
    'password' => bcrypt('password'),
    'role'     => 'super_admin',
]);
```

### 6. Configure CORS
In `config/cors.php`, set:
```php
'allowed_origins' => ['http://localhost:3000'],
```

### 7. Add API routes
In `bootstrap/app.php`, ensure API routes are loaded:
```php
->withRouting(
    api: __DIR__.'/../routes/api.php',
    apiPrefix: 'api',
)
```

### 8. Start the server
```bash
php artisan serve
# Runs on http://localhost:8000
```

---

## Frontend (React)

### 1. Install dependencies
```bash
cd frontend
npm install
```

### 2. Configure environment
```bash
cp .env.example .env
```

`.env`:
```
VITE_API_URL=http://localhost:8000/api/v1
```

### 3. Start development server
```bash
npm run dev
# Runs on http://localhost:3000
```

### 4. Build for production
```bash
npm run build
# Output in dist/ folder
```

---

## VPS Deployment

### Backend
```bash
# On your VPS (Ubuntu 22.04)
apt install php8.2 php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml nginx mysql-server

# Clone project
git clone <your-repo> /var/www/agency-pm

# Setup backend
cd /var/www/agency-pm/backend
composer install --no-dev
cp .env.example .env
php artisan key:generate
php artisan migrate --force

# Set permissions
chown -R www-data:www-data /var/www/agency-pm/backend
chmod -R 775 storage bootstrap/cache
```

### Nginx config (backend)
```nginx
server {
    listen 80;
    server_name api.yourdomain.com;
    root /var/www/agency-pm/backend/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
```

### Frontend
```bash
cd /var/www/agency-pm/frontend
npm install
npm run build

# Serve the dist/ folder with Nginx
```

---

## File structure
```
agency-pm/
├── backend/
│   ├── app/
│   │   ├── Http/Controllers/Api/
│   │   │   ├── AuthController.php
│   │   │   ├── ProjectController.php
│   │   │   ├── TaskController.php
│   │   │   └── UserController.php
│   │   └── Models/
│   │       ├── User.php
│   │       ├── Project.php
│   │       ├── Task.php
│   │       ├── TaskComment.php
│   │       └── TaskTimeLog.php
│   ├── database/migrations/
│   └── routes/api.php
└── frontend/
    └── src/
        ├── api/          # Axios service layer
        ├── context/      # Auth context
        ├── components/   # Reusable UI
        └── pages/        # Route pages
            ├── Dashboard.jsx
            ├── Login.jsx
            ├── Projects/
            └── Tasks/
```
