# Blog Module Implementation Guide

## Overview
The Blog module has been fully updated with a `BlogDetail` model to separate detailed blog content from the main blog metadata. This provides better organization and performance optimization.

## Database Structure

### blogs table
- `id` - Primary key
- `uid` - Unique identifier (UUID)
- `title` - Blog title
- `slug` - URL slug (unique)
- `description` - Short description
- `theme_category_id` - Associated theme category
- `media_id` - Featured image reference
- `type` - Blog type (NEWS, EVENT, etc.)
- `status` - Status (ACTIVE, INACTIVE)
- `timestamps` - created_at, updated_at
- `soft_deletes` - Soft deletion support

### blog_details table
- `id` - Primary key
- `blog_id` - Foreign key to blogs table (cascade delete)
- `language_id` - Associated language (nullable on delete)
- `title` - Detail title
- `slug` - Detail slug (unique)
- `description` - Full blog content
- `media_id` - Additional media (nullable on delete)
- `view_count` - Number of views (default 0)
- `timestamps` - created_at, updated_at
- `soft_deletes` - Soft deletion support

## Models

### Blog Model
Location: `Website/Blog/app/Models/Blog.php`

**Relationships:**
- `detail()` - hasOne BlogDetail
- `media()` - belongsTo Media
- `themeCategory()` - belongsTo ThemeCategory
- `language()` - belongsTo Language (removed from current migration)

**Methods:**
- `modelFilter()` - Uses BlogFilter for querying
- `getTypeLabelAttribute()` - Returns type label

### BlogDetail Model
Location: `Website/Blog/app/Models/BlogDetail.php`

**Relationships:**
- `blog()` - belongsTo Blog
- `media()` - belongsTo Media
- `language()` - belongsTo Language

**Methods:**
- `modelFilter()` - Uses BlogDetailFilter for querying
- `incrementViewCount()` - Increments view counter

## Filters

### BlogFilter
Location: `Website/Blog/app/ModelFilters/BlogFilter.php`

Searches across `title`, `description`, and `type` fields.

### BlogDetailFilter
Location: `Website/Blog/app/ModelFilters/BlogDetailFilter.php`

**Filter Methods:**
- `search($value)` - Search title, description, slug
- `language($value)` - Filter by language_id
- `blog($value)` - Filter by blog_id
- `viewCountMin($value)` - Minimum view count
- `viewCountMax($value)` - Maximum view count

## Transformers/Resources

### BlogResource
Location: `Website/Blog/app/Transformers/BlogResource.php`

Returns blog with basic details and included relationships.

### BlogDetailResource
Location: `Website/Blog/app/Transformers/BlogDetailResource.php`

Returns blog detail with language and media.

### BlogBlogResource
Location: `Website/Blog/app/Transformers/BlogBlogResource.php`

Extended resource for blog details with language and media relationships.

## Controllers

### BlogController
Location: `Website/Blog/app/Http/Controllers/BlogController.php`

**Methods:**
- `index()` - List blogs with pagination and filtering
- `create()` - Show create form
- `store()` - Store new blog
- `show()` - Display blog with details
- `edit()` - Show edit form
- `update()` - Update blog
- `destroy()` - Delete blog

### BlogDetailController
Location: `Website/Blog/app/Http/Controllers/BlogDetailController.php`

**Methods:**
- `show($blogId)` - Get blog detail by blog ID (increments views)
- `update($blogId)` - Update blog detail
- `index()` - List blog details with filtering

## Routes

### API Routes
Location: `Website/Blog/routes/api.php`

```
GET  /v1/blogs                 - List blogs
POST /v1/blogs                 - Create blog
GET  /v1/blogs/{id}            - Show blog
PUT  /v1/blogs/{id}            - Update blog
DELETE /v1/blogs/{id}          - Delete blog

GET  /v1/blogs/{blogId}/detail - Get blog detail
PUT  /v1/blogs/{blogId}/detail - Update blog detail
GET  /v1/blog-details          - List all blog details
```

## Seeders

### BlogDatabaseSeeder
Location: `Website/Blog/database/seeders/BlogDatabaseSeeder.php`

Creates sample blogs with corresponding blog details and relationships.

## Factories

### BlogDetailFactory
Location: `Website/Blog/database/factories/BlogDetailFactory.php`

Generates test data for blog details with:
- Random titles and slugs
- Faker paragraphs for content
- Random view counts

## Usage Examples

### Creating a Blog with Detail
```php
$blog = Blog::create([
    'title' => 'My Blog Post',
    'slug' => 'my-blog-post',
    'description' => 'Short description',
    'type' => 1,
    'status' => 1,
    'media_id' => 1,
]);

BlogDetail::create([
    'blog_id' => $blog->id,
    'language_id' => 1,
    'title' => 'My Blog Post',
    'slug' => 'my-blog-post-detail',
    'description' => 'Full detailed content here...',
    'media_id' => 1,
]);
```

### Retrieving Blog with Detail
```php
$blog = Blog::with('detail', 'media')->find(1);
echo $blog->detail->description;
```

### Filtering Blog Details
```php
$details = BlogDetail::filter([
    'search' => 'Laravel',
    'language' => 1,
    'viewCountMin' => 10
])->paginate();
```

### Incrementing View Count
```php
$blogDetail = BlogDetail::find(1);
$blogDetail->incrementViewCount();
```

### API Usage
```bash
# Get blog detail (increments view count)
curl -X GET http://localhost:8000/v1/blogs/1/detail \
  -H "Authorization: Bearer {token}"

# Update blog detail
curl -X PUT http://localhost:8000/v1/blogs/1/detail \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Updated Title",
    "description": "Updated content",
    "language_id": 1
  }'

# List blog details with filters
curl -X GET "http://localhost:8000/v1/blog-details?language_id=1&search=Laravel" \
  -H "Authorization: Bearer {token}"
```

## Migration

Run migrations using:
```bash
php artisan tenants:migrate
```

Or for development:
```bash
php artisan dev:i
```

The migrations create both `blogs` and `blog_details` tables with proper relationships and constraints.
