# Product Image Editor - AI-Powered Image Editing Platform

A modern Laravel application that provides AI-powered image editing services for products and barber/stylist services. The application features a complete membership system, multilingual support (English/Romanian), and integration with OpenAI and Replicate APIs.

## Features

### 🎨 Product Image Editing
- AI-powered image enhancement using OpenAI API
- Multiple predefined prompts for different editing styles
- Mask support for precise editing
- High-quality output with professional results

### 💇‍♂️ Barber/Stylist Services
- Hair style swapping using Replicate API
- Upload person image and desired hair style
- AI-powered face preservation and background restoration
- Professional hair swap results

### 👥 Membership System
- **Free Plan**: 2 free image edits + 2 free barber edits
- **Basic Plan**: 20 image edits + 10 barber edits per month ($19.99)
- **Pro Plan**: Unlimited image edits + 50 barber edits per month ($49.99)

### 🌍 Multilingual Support
- English and Romanian languages
- Complete UI translation
- User language preference storage

### 🔐 Authentication & Security
- Laravel Sanctum for API authentication
- Secure user registration and login
- Role-based access control (User/Admin)

### 📊 Admin Panel
- User management
- Edit history and statistics
- Membership plan management
- System monitoring and analytics

### 🎨 Modern UI/UX
- Responsive design with Tailwind CSS
- Glass morphism effects
- Smooth animations and transitions
- Mobile-friendly interface

## Technology Stack

- **Backend**: Laravel 12 (PHP)
- **Frontend**: Vanilla JavaScript, Tailwind CSS
- **Database**: SQLite (can be changed to MySQL/PostgreSQL)
- **Authentication**: Laravel Sanctum
- **APIs**: OpenAI API, Replicate API
- **File Storage**: Laravel Storage (local)

## Prerequisites

- PHP 8.2 or higher
- Composer
- Node.js (optional, for asset compilation)

## Installation

1. **Clone the repository**
   ```bash
   git clone <repository-url>
   cd product-image-editor
   ```

2. **Install PHP dependencies**
   ```bash
   composer install
   ```

3. **Environment setup**
   ```bash
   cp .env.example .env
   php artisan key:generate
   ```

4. **Configure environment variables**
   Edit `.env` file and add your API keys:
   ```env
   OPENAI_API_KEY=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
   REPLICATE_API_KEY=your-replicate-api-key-here
   ```

5. **Database setup**
   ```bash
   php artisan migrate:fresh --seed
   ```

6. **Create storage link**
   ```bash
   php artisan storage:link
   ```

7. **Start the development server**
   ```bash
   php artisan serve
   ```

8. **Access the application**
   Open your browser and navigate to `http://localhost:8000`

## Default Admin Account

After running the seeders, you can log in with the admin account:
- **Email**: admin@productimage.com
- **Password**: admin123

## API Endpoints

### Authentication
- `POST /api/register` - User registration
- `POST /api/login` - User login
- `POST /api/logout` - User logout
- `GET /api/profile` - Get user profile
- `PUT /api/profile` - Update user profile

### Image Editing
- `GET /api/image-edits` - Get user's image edits
- `POST /api/image-edits` - Create new image edit
- `GET /api/image-edits/prompts` - Get available prompts
- `GET /api/image-edits/{id}` - Get specific image edit

### Barber Services
- `GET /api/barber-edits` - Get user's barber edits
- `POST /api/barber-edits` - Create new barber edit
- `GET /api/barber-edits/hair-styles` - Get hair style suggestions
- `GET /api/barber-edits/{id}` - Get specific barber edit

### Admin (Admin only)
- `GET /api/admin/dashboard` - Admin dashboard stats
- `GET /api/admin/users` - Get all users
- `GET /api/admin/image-edits` - Get all image edits
- `GET /api/admin/barber-edits` - Get all barber edits
- `GET /api/admin/membership-plans` - Get membership plans
- `PUT /api/admin/membership-plans/{id}` - Update membership plan

## Database Structure

### Users Table
- Basic user information
- Role (user/admin)
- Language preference
- Free usage tracking

### Membership Plans Table
- Plan details with multilingual support
- Pricing and limits
- Stripe integration support

### User Memberships Table
- User subscription tracking
- Usage statistics
- Subscription status

### Image Edits Table
- Image editing history
- API responses
- Processing status and timing

### Barber Edits Table
- Hair swap history
- Person and hair style images
- Processing results

## File Structure

```
product-image-editor/
├── app/
│   ├── Http/Controllers/
│   │   ├── AuthController.php
│   │   ├── ImageEditController.php
│   │   ├── BarberEditController.php
│   │   └── AdminController.php
│   ├── Models/
│   │   ├── User.php
│   │   ├── MembershipPlan.php
│   │   ├── UserMembership.php
│   │   ├── ImageEdit.php
│   │   └── BarberEdit.php
│   ├── Services/
│   │   ├── OpenAIService.php
│   │   └── ReplicateService.php
│   └── Http/Middleware/
│       └── AdminMiddleware.php
├── database/
│   ├── migrations/
│   └── seeders/
├── public/
│   ├── index.html
│   └── js/
│       └── app.js
├── routes/
│   └── api.php
└── storage/
    └── app/public/
```

## Usage

### For Users

1. **Register/Login**: Create an account or log in to existing account
2. **Choose Service**: Select between product image editing or barber services
3. **Upload Images**: Upload your images according to the service requirements
4. **Select Options**: Choose prompts or hair styles
5. **Process**: Submit for AI processing
6. **View Results**: Check your recent edits for completed results

### For Admins

1. **Login**: Use admin credentials to access admin panel
2. **Monitor**: View system statistics and user activity
3. **Manage**: Handle users, edits, and membership plans
4. **Analytics**: Review usage patterns and revenue

## Configuration

### OpenAI API
The application uses OpenAI's image editing API for product image enhancement. Make sure to:
- Set your OpenAI API key in the `.env` file
- Ensure you have sufficient API credits
- Monitor API usage for cost management

### Replicate API
For barber services, the application uses Replicate's hair swap models. Configure:
- Replicate API key in `.env` file
- Model version in `ReplicateService.php`
- Polling intervals for long-running predictions

### File Storage
The application stores uploaded images locally. For production:
- Consider using cloud storage (AWS S3, Google Cloud Storage)
- Implement image optimization
- Set up proper backup strategies

## Security Considerations

- All API endpoints are protected with authentication
- Admin routes require admin privileges
- File uploads are validated and sanitized
- API keys are stored securely in environment variables
- CSRF protection is enabled for web routes

## Performance Optimization

- Image processing is handled asynchronously
- Database queries are optimized with proper indexing
- API responses are cached where appropriate
- File storage is organized efficiently

## Troubleshooting

### Common Issues

1. **API Key Errors**: Ensure your OpenAI and Replicate API keys are correctly set
2. **File Upload Issues**: Check storage permissions and disk space
3. **Database Errors**: Verify database configuration and run migrations
4. **CORS Issues**: Configure CORS settings for cross-origin requests

### Debug Mode

Enable debug mode in `.env` for detailed error messages:
```env
APP_DEBUG=true
```

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request

## License

This project is licensed under the MIT License.

## Support

For support and questions:
- Create an issue in the repository
- Contact the development team
- Check the documentation

## Roadmap

- [ ] Payment integration (Stripe)
- [ ] Advanced image filters
- [ ] Batch processing
- [ ] Mobile app
- [ ] Additional language support
- [ ] Advanced admin analytics
- [ ] API rate limiting
- [ ] Webhook support
