# Database Schema Documentation

This document outlines the database schema for the GoHorseJobs platform.

## Entity Relationship Diagram

```mermaid
erDiagram
    users {
        int id PK
        varchar identifier UK
        varchar password_hash
        varchar role
        varchar full_name
        varchar language
        boolean active
        timestamp created_at
        timestamp updated_at
        timestamp last_login_at
    }
    
    companies {
        int id PK
        varchar name
        varchar slug UK
        varchar type
        varchar document
        varchar address
        int region_id FK
        int city_id FK
        varchar phone
        varchar email
        varchar website
        varchar logo_url
        text description
        boolean active
        boolean verified
        timestamp created_at
        timestamp updated_at
    }
    
    jobs {
        int id PK
        int company_id FK
        int created_by FK
        varchar title
        text description
        decimal salary_min
        decimal salary_max
        varchar salary_type
        varchar currency
        varchar employment_type
        varchar work_mode
        varchar working_hours
        varchar location
        int region_id FK
        int city_id FK
        jsonb requirements
        jsonb benefits
        boolean visa_support
        varchar language_level
        varchar status
        boolean is_featured
        timestamp created_at
        timestamp updated_at
    }
    
    applications {
        int id PK
        int job_id FK
        int user_id FK
        varchar name
        varchar email
        varchar phone
        varchar line_id
        varchar whatsapp
        text message
        varchar resume_url
        jsonb documents
        varchar status
        text notes
        timestamp created_at
        timestamp updated_at
    }
    
    regions {
        int id PK
        varchar name
        varchar country_code
        varchar code
    }
    
    cities {
        int id PK
        int region_id FK
        varchar name
    }
    
    user_companies {
        int id PK
        int user_id FK
        int company_id FK
        varchar role
        jsonb permissions
    }
    
    favorite_jobs {
        int id PK
        int user_id FK
        int job_id FK
    }
    
    job_payments {
        uuid id PK
        uuid job_id FK
        uuid user_id FK
        varchar stripe_session_id
        varchar stripe_payment_intent
        decimal amount
        varchar currency
        varchar status
        varchar billing_type
        varchar billing_name
        varchar billing_email
        int duration_days
        timestamp created_at
        timestamp paid_at
    }
    
    job_posting_prices {
        int id PK
        varchar name
        decimal price
        varchar currency
        int duration_days
        boolean is_featured
        varchar stripe_price_id
        boolean active
    }

    users ||--o{ user_companies : "belongs to"
    companies ||--o{ user_companies : "has members"
    companies ||--o{ jobs : "posts"
    users ||--o{ jobs : "creates"
    jobs ||--o{ applications : "receives"
    users ||--o{ applications : "submits"
    jobs ||--o{ job_payments : "has payment"
    companies ||--o{ user_companies : "has members"
    companies ||--o{ jobs : "posts"
    users ||--o{ jobs : "creates"
    jobs ||--o{ applications : "receives"
    users ||--o{ applications : "submits"
    regions ||--o{ cities : "contains"
    regions ||--o{ companies : "located in"
    cities ||--o{ companies : "located in"
    regions ||--o{ jobs : "located in"
    cities ||--o{ jobs : "located in"
    users ||--o{ favorite_jobs : "saves"
    jobs ||--o{ favorite_jobs : "saved by"
```

## Core Tables

### Users (`users`)
Represents system users including Candidates, Recruiters, and Admins.

| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| identifier | VARCHAR | Unique, Email or Username |
| password_hash | VARCHAR | Bcrypt hash |
| role | VARCHAR | `superadmin`, `companyAdmin`, `recruiter`, `jobSeeker` |
| full_name | VARCHAR | Display name |
| language | VARCHAR | Default: 'pt' |
| active | BOOLEAN | Account status |
| created_at | TIMESTAMP | Creation time |
| updated_at | TIMESTAMP | Last update |
| last_login_at | TIMESTAMP | Last login |

---

### Companies (`companies`)
Represents employer organizations.

| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| name | VARCHAR | Company name |
| slug | VARCHAR | URL-friendly identifier |
| type | VARCHAR | Company type |
| document | VARCHAR | Houjin Bangou / CNPJ |
| address | VARCHAR | Physical address |
| region_id | INTEGER | FK → regions.id |
| city_id | INTEGER | FK → cities.id |
| phone, email, website | VARCHAR | Contact info |
| logo_url | VARCHAR | Logo image URL |
| description | TEXT | Company description |
| active | BOOLEAN | Active status |
| verified | BOOLEAN | Verification status |

---

### Jobs (`jobs`)
Represents job postings.

| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| company_id | INTEGER | FK → companies.id |
| created_by | INTEGER | FK → users.id |
| title | VARCHAR | Job title |
| description | TEXT | Full job description |
| salary_min | DECIMAL | Minimum salary |
| salary_max | DECIMAL | Maximum salary |
| salary_type | VARCHAR | `hourly`, `daily`, `weekly`, `monthly`, `yearly` |
| currency | VARCHAR | `BRL`, `USD`, `EUR`, `GBP`, `JPY` |
| employment_type | VARCHAR | `full-time`, `part-time`, `contract`, `temporary`, `training`, `voluntary`, `permanent`, `dispatch` |
| work_mode | VARCHAR | `onsite`, `hybrid`, `remote` |
| working_hours | VARCHAR | e.g. "9:00-18:00" |
| location | VARCHAR | Location string |
| region_id | INTEGER | FK → regions.id |
| city_id | INTEGER | FK → cities.id |
| requirements | JSONB | Skills/requirements array |
| benefits | JSONB | Benefits array |
| visa_support | BOOLEAN | Visa sponsorship |
| language_level | VARCHAR | `N5`, `N4`, `N3`, `N2`, `N1`, `beginner`, `none` |
| status | VARCHAR | `draft`, `open`, `closed`, `published`, `paused`, `expired`, `archived`, `reported`, `review` |
| is_featured | BOOLEAN | Featured job flag |

---

### Applications (`applications`)
Represents job applications.

| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| job_id | INTEGER | FK → jobs.id |
| user_id | INTEGER | FK → users.id (nullable for guests) |
| name | VARCHAR | Applicant name |
| email | VARCHAR | Contact email |
| phone | VARCHAR | Phone number |
| line_id | VARCHAR | LINE ID |
| whatsapp | VARCHAR | WhatsApp number |
| message | TEXT | Cover message |
| resume_url | VARCHAR | Resume file URL |
| documents | JSONB | Additional documents |
| status | VARCHAR | `pending`, `reviewed`, `shortlisted`, `rejected`, `hired` |
| notes | TEXT | Recruiter notes |

---

## Reference Tables

### Regions (`regions`)
| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| name | VARCHAR | Region name |
| country_code | VARCHAR | e.g. "JP", "BR" |
| code | VARCHAR | Region code |

### Cities (`cities`)
| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| region_id | INTEGER | FK → regions.id |
| name | VARCHAR | City name |

---

## Junction Tables

### User Companies (`user_companies`)
Maps users to companies (N:M relationship).

| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| user_id | INTEGER | FK → users.id |
| company_id | INTEGER | FK → companies.id |
| role | VARCHAR | `companyAdmin`, `recruiter` |
| permissions | JSONB | Custom permissions |

### Favorite Jobs (`favorite_jobs`)
| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| user_id | INTEGER | FK → users.id |
| job_id | INTEGER | FK → jobs.id |

---

## Audit Tables

### Login Audits (`login_audits`)
| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| user_id | VARCHAR | User identifier |
| identifier | VARCHAR | Login identifier used |
| ip_address | VARCHAR | Client IP |
| user_agent | VARCHAR | Browser/client info |
| created_at | TIMESTAMP | Login time |

### Password Resets (`password_resets`)
| Column | Type | Description |
|--------|------|-------------|
| id | INTEGER | Primary key |
| user_id | INTEGER | FK → users.id |
| token | VARCHAR | Reset token |
| expires_at | TIMESTAMP | Token expiry |
| used | BOOLEAN | Token used status |
