# Offer Controllers Implementation Summary

## Files Created

### 1. Filter
- **app/Filter/OfferFilter.php**
  - Filters: `product`, `vendor`, `is_active`, `expired`, `from`, `to`
  - Supports filtering by product, vendor, active status, expiration, and date range (from/to)

### 2. Admin Controllers & Resources
- **app/Http/Controllers/Api/Dashboard/Admin/Offer/OfferController.php**
  - Methods:
    - `index()` - List all offers with filtering and statistics
    - `show($id)` - Get specific offer details
    - `toggleActive($id)` - Toggle offer active status
    - `deactivate($id)` - Deactivate an offer
    - `statistics()` - Get offer statistics (total, active, expired, upcoming)

- **app/Http/Resources/Api/Dashboard/Admin/Offer/OfferResource.php**
  - List resource with product and vendor info

- **app/Http/Resources/Api/Dashboard/Admin/Offer/OfferDetailsResource.php**
  - Detailed resource with full product and vendor information

- **app/Http/Resources/Api/Dashboard/Admin/Offer/OfferDiscountResource.php**
  - Discount details resource (separate from Product folder)

### 3. Vendor Controllers & Resources
- **app/Http/Controllers/Api/Dashboard/Vendor/Offer/OfferController.php**
  - Methods (scoped to authenticated vendor):
    - `index()` - List vendor's offers with filtering and statistics
    - `show($id)` - Get specific offer details (vendor's only)
    - `toggleActive($id)` - Toggle offer active status (vendor's only)
    - `deactivate($id)` - Deactivate an offer (vendor's only)
    - `statistics()` - Get vendor's offer statistics

- **app/Http/Resources/Api/Dashboard/Vendor/Offer/OfferResource.php**
  - List resource with product info

- **app/Http/Resources/Api/Dashboard/Vendor/Offer/OfferDetailsResource.php**
  - Detailed resource with full product information

- **app/Http/Resources/Api/Dashboard/Vendor/Offer/OfferDiscountResource.php**
  - Discount details resource (separate from Product folder)

### 4. Model Update
- **app/Models/Offer.php**
  - Added `scopeFilter()` method for filtering support

## Filter Usage Examples

### Filter by Product ID
```json
{
  "filters": {
    "product": 123
  }
}
```

### Filter by Vendor
```json
{
  "filters": {
    "vendor": 456
  }
}
```

### Filter by Active Status
```json
{
  "filters": {
    "is_active": true
  }
}
```

### Filter by Expired Status
```json
{
  "filters": {
    "expired": true
  }
}
```

### Filter by Date Range (From and To)
```json
{
  "filters": {
    "from": "2026-01-01",
    "to": "2026-12-31"
  }
}
```

### Combined Filters
```json
{
  "filters": {
    "vendor": 456,
    "is_active": true,
    "from": "2026-01-01",
    "to": "2026-12-31"
  },
  "per_page": 20
}
```

## API Endpoints (Routes Added)

### Admin Routes (routes/api/dashboard/admin.php)
```php
Route::controller(OfferController::class)->prefix('offers')->group(function () {
    Route::get('', 'index')->name('offers.index');
    Route::get('statistics', 'statistics')->name('offers.statistics');
    Route::get('{id}', 'show')->name('offers.show');
    Route::post('{id}/toggle-active', 'toggleActive')->name('offers.toggle-active');
    Route::post('{id}/deactivate', 'deactivate')->name('offers.deactivate');
});
```

**Endpoints:**
- `GET /api/dashboard/admin/offers` - List all offers with filtering
- `GET /api/dashboard/admin/offers/statistics` - Get offer statistics
- `GET /api/dashboard/admin/offers/{id}` - Get specific offer details
- `POST /api/dashboard/admin/offers/{id}/toggle-active` - Toggle offer active status
- `POST /api/dashboard/admin/offers/{id}/deactivate` - Deactivate an offer

### Vendor Routes (routes/api/dashboard/vendor.php)
```php
Route::controller(OfferController::class)->prefix('offers')->group(function () {
    Route::get('', 'index')->name('offers.index');
    Route::get('statistics', 'statistics')->name('offers.statistics');
    Route::get('{id}', 'show')->name('offers.show');
    Route::post('{id}/toggle-active', 'toggleActive')->name('offers.toggle-active');
    Route::post('{id}/deactivate', 'deactivate')->name('offers.deactivate');
});
```

**Endpoints:**
- `GET /api/dashboard/vendor/offers` - List vendor's offers with filtering
- `GET /api/dashboard/vendor/offers/statistics` - Get vendor's offer statistics
- `GET /api/dashboard/vendor/offers/{id}` - Get specific offer details (vendor's only)
- `POST /api/dashboard/vendor/offers/{id}/toggle-active` - Toggle offer active status (vendor's only)
- `POST /api/dashboard/vendor/offers/{id}/deactivate` - Deactivate an offer (vendor's only)

## Features

### Admin Features
- View all offers across all vendors
- Filter by product, vendor, active status, expiration, date range
- View detailed offer information including vendor details
- Toggle offer active status
- Deactivate offers
- View statistics (total, active, expired, upcoming)

### Vendor Features
- View only their own offers
- Filter by product, active status, expiration, date range
- View detailed offer information
- Toggle their own offer active status
- Deactivate their own offers
- View their own statistics (total, active, expired, upcoming)

## Response Examples

### List Response (with statistics and remaining time)
```json
{
  "status": "success",
  "message": "success",
  "data": {
    "offers": [
      {
        "id": 1,
        "product_id": 123,
        "product_name": "Product Name",
        "vendor_id": 456,
        "vendor_name": "Vendor Name",
        "discount": [...],
        "start_at": "2026-01-01",
        "end_at": "2026-12-31",
        "is_active": true,
        "is_expired": false,
        "remaining_time": {
          "value": 5,
          "unit": "days",
          "formatted": "5 days"
        },
        "created_at": "2026-01-01",
        "sold_items": 150
      }
    ],
    "total": 150,
    "active": 80,
    "expired": 70
  }
}
```

### Remaining Time Field
The `remaining_time` field shows how much time is left until the offer expires:
- If more than 1 day remaining: shows days (e.g., "5 days")
- If less than 1 day remaining: shows hours (e.g., "12 hours")
- If offer is expired or has no end date: returns `null`

**Structure:**
```json
{
  "value": 5,           // numeric value
  "unit": "days",       // "day", "days", "hour", or "hours"
  "formatted": "5 days" // translated formatted string
}
```

### Statistics Response
```json
{
  "status": "success",
  "message": "Statistics retrieved successfully",
  "data": {
    "total": 150,
    "active": 80,
    "expired": 70,
    "upcoming": 10
  }
}
```
