# Database Seeders Documentation

This document provides information about the database seeders available in the BYRC Backend application.

## Available Seeders

### 1. RolePermissionSeeder
Seeds the roles and permissions for the application using Spatie Laravel Permission package.

**Tables Affected:**
- `roles`
- `permissions`
- `role_has_permissions`

### 2. PostSeeder
Seeds the posts table with sample blog posts, news articles, and events.

**Tables Affected:**
- `posts`

**Foreign Key Relations:**
- `user_id` → references `users.id`

**Features:**
- Creates 9 sample posts (3 blogs, 2 news, 3 events, 1 draft)
- Automatically creates a default user if no users exist
- Randomly assigns posts to existing users
- Includes diverse content types with proper metadata
- Contains public and draft visibility examples

**Sample Data Includes:**
- Blog posts about technology, productivity, and digital transformation
- News announcements about product updates and partnerships
- Event posts with dates, times, and locations
- Draft posts for testing purposes

### 3. ServiceSeeder
Seeds services along with their related service links and service videos.

**Tables Affected:**
- `services`
- `service_links`
- `service_videos`

**Foreign Key Relations:**
- `service_links.service_id` → references `services.id`
- `service_videos.service_id` → references `services.id`

**Features:**
- Creates 6 comprehensive services:
  1. Cloud Computing Solutions
  2. Data Analytics & Business Intelligence
  3. Cybersecurity Services
  4. Custom Software Development
  5. Digital Marketing Services
  6. IT Consulting & Strategy

- For each service, creates:
  - 2 national service links
  - 2 international service links
  - 3 service videos

**Total Records Created:**
- 6 Services
- 24 Service Links (4 per service)
- 18 Service Videos (3 per service)

## Running Seeders

### Run All Seeders
```bash
php artisan db:seed
```

### Run Specific Seeder
```bash
# Run only post seeder
php artisan db:seed --class=PostSeeder

# Run only service seeder
php artisan db:seed --class=ServiceSeeder

# Run only role permission seeder
php artisan db:seed --class=RolePermissionSeeder
```

### Fresh Migration with Seeding
```bash
# Drop all tables, re-run migrations, and seed
php artisan migrate:fresh --seed
```

## Seeding Order

The seeders run in the following order (as defined in `DatabaseSeeder.php`):

1. **RolePermissionSeeder** - Creates roles and permissions first
2. **PostSeeder** - Creates posts (requires users to exist)
3. **ServiceSeeder** - Creates services with links and videos

## Database Schema Overview

### Posts Table Structure
```
posts
├── id (primary key)
├── title
├── slug (unique)
├── post_type (enum: news, event, blog)
├── summary
├── content
├── feature_image
├── caption
├── author_name
├── author_designation
├── meta_title
├── meta_description
├── meta_keywords
├── language
├── categories (json)
├── tags (json)
├── event_date
├── event_time
├── event_location
├── visibility (enum: public, private, draft)
├── user_id (foreign key → users.id)
├── url
├── created_at
└── updated_at
```

### Services Table Structure
```
services
├── id (primary key)
├── title
├── excerpt
├── slug (unique)
├── description
├── feature_image
├── primary_btn_text
├── secondary_btn_text
├── status (boolean)
├── created_at
└── updated_at
```

### Service Links Table Structure
```
service_links
├── id (primary key)
├── service_id (foreign key → services.id)
├── type (enum: national, international)
├── heading
├── description
├── url
├── primary_btn_text
├── primary_btn_url
├── status (boolean)
├── created_at
└── updated_at
```

### Service Videos Table Structure
```
service_videos
├── id (primary key)
├── service_id (foreign key → services.id)
├── heading
├── description
├── url
├── status (boolean)
├── created_at
└── updated_at
```

## Customization

### Adding More Posts
Edit `database/seeders/PostSeeder.php` and add entries to the `$posts` array.

### Adding More Services
Edit `database/seeders/ServiceSeeder.php` and add entries to the `$services` array.

### Modifying Service Links/Videos
Edit the `createServiceLinks()` and `createServiceVideos()` methods in `ServiceSeeder.php`.

## Important Notes

1. **User Dependency**: PostSeeder requires at least one user to exist. If no users are found, it automatically creates a default admin user.

2. **Foreign Key Constraints**: Ensure migrations are run before seeding to avoid foreign key constraint errors.

3. **Unique Constraints**: Slugs must be unique. If re-seeding, either truncate tables or use `migrate:fresh --seed`.

4. **Service Relations**: When a service is deleted, all related service_links and service_videos are automatically deleted due to `onDelete('cascade')` in migrations.

5. **Post Relations**: When a user is deleted, all their posts are automatically deleted due to `onDelete('cascade')` in migrations.

## Troubleshooting

### Error: SQLSTATE[23000]: Integrity constraint violation
**Solution**: Run `php artisan migrate:fresh --seed` to drop all tables and recreate them with fresh data.

### Error: Class 'PostSeeder' not found
**Solution**: Run `composer dump-autoload` to regenerate the autoload files.

### Error: User not found
**Solution**: Ensure RolePermissionSeeder runs before PostSeeder, or manually create users first.

## Testing Data Access

After seeding, you can test the data:

```bash
# Check posts
php artisan tinker
>>> App\Models\Post::count();
>>> App\Models\Post::with('user')->first();

# Check services
>>> App\Models\Service::count();
>>> App\Models\Service::with(['serviceLinks', 'serviceVideos'])->first();

# Check relationships
>>> $service = App\Models\Service::first();
>>> $service->serviceLinks()->count();
>>> $service->serviceVideos()->count();
```

## API Endpoints to Test Seeded Data

### Posts
```
GET /api/all-posts?type=all&status=public&per_page=10&page=1
GET /api/show-post/{slug}
```

### Services
```
GET /api/services
GET /api/services/{id}
GET /api/services/{serviceId}/links
GET /api/services/{serviceId}/videos
```

