# Installation Guide

## Requirements

- PHP 8.2 or higher
- MySQL 5.7+ or MariaDB 10.3+
- Apache with mod_rewrite enabled
- PHP extensions: pdo, pdo_mysql, mbstring, openssl, fileinfo

## Quick Setup

### 1. Clone / Copy Files

Place the project files in your web server's document root or a subdirectory:

```
/htdocs/AiWebReporter/    (XAMPP)
/public_html/             (cPanel)
```

### 2. Create Database

```sql
CREATE DATABASE aiwebreporter CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

### 3. Run Migrations

Execute the SQL migration files in order via phpMyAdmin or MySQL CLI:

```
database/migrations/001_schema.sql
database/migrations/002_categories_seed.sql
database/migrations/003_bot_system.sql
database/migrations/004_newsletter_system.sql
database/migrations/005_static_pages.sql
```

### 4. Configure Application

Copy or edit `config/config.php`:

```php
return [
    'app' => [
        'name'     => 'AI Web Reporter',
        'url'      => 'http://localhost/AiWebReporter',  // No trailing slash
        'debug'    => true,       // Set false in production
        'timezone' => 'UTC',
    ],
    'db' => [
        'host'     => 'localhost',
        'name'     => 'aiwebreporter',
        'user'     => 'root',
        'pass'     => '',
        'charset'  => 'utf8mb4',
    ],
    'session' => [
        'name'     => 'AIWR_SESSION',
        'lifetime' => 7200,
        'secure'   => false,      // Set true with HTTPS
        'httponly'  => true,
        'samesite' => 'Lax',
    ],
    'mail' => [
        'host'       => '',       // SMTP server
        'port'       => 587,
        'username'   => '',
        'password'   => '',
        'encryption' => 'tls',
        'from_email' => 'noreply@yourdomain.com',
        'from_name'  => 'AI Web Reporter',
    ],
];
```

### 5. Set Permissions

Ensure the `public/uploads/` directory is writable by the web server:

```bash
chmod 755 public/uploads/
```

### 6. Create Admin Account

The first migration seeds a default admin account. Check `001_schema.sql` for credentials, or insert manually:

```sql
INSERT INTO users (role_id, username, email, password_hash, display_name, status)
VALUES (1, 'admin', 'admin@example.com', '$2y$12$...', 'Admin', 'approved');
```

Generate the password hash with: `php -r "echo password_hash('YourPassword', PASSWORD_BCRYPT, ['cost' => 12]);"`

### 7. Configure .htaccess (Apache)

The `public/.htaccess` file should contain:

```apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
```

### 8. Verify Installation

- Visit `http://yoursite/` — you should see the homepage
- Visit `http://yoursite/login` — log in with the admin account
- Check the admin dashboard for any errors

## Directory Structure

```
AiWebReporter/
├── app/
│   ├── controllers/        # Request handlers
│   ├── helpers/            # Utility functions
│   ├── middleware/          # Auth middleware
│   ├── models/             # Database models
│   ├── services/           # Mail and newsletter services
│   └── views/              # PHP templates
│       ├── admin/          # Admin panel views
│       ├── auth/           # Login/register views
│       ├── components/     # Reusable UI components
│       ├── emails/         # Email templates
│       ├── errors/         # Error pages (403, 404, 500)
│       ├── layouts/        # Header/footer layouts
│       ├── newsletter/     # Newsletter public views
│       └── public/         # Public page views
├── config/
│   └── config.php          # Application configuration
├── cron/
│   └── newsletter_send.php # Newsletter queue processor
├── database/
│   └── migrations/         # SQL migration files
├── docs/                   # Documentation
├── lib/
│   └── PHPMailer/          # PHPMailer library
├── public/
│   ├── css/                # Stylesheets
│   ├── images/             # Static images
│   ├── js/                 # JavaScript files
│   ├── uploads/            # User-uploaded files
│   ├── .htaccess           # URL rewrite rules
│   ├── index.php           # Front controller
│   └── robots.txt          # Search engine directives
└── routes/
    └── web.php             # Route definitions
```
