<?php
namespace App\Http\Controllers\Api\V1;
use App\Models\Merchant;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
final class SubMerchantController {
 public function index(Request $r){return ApiResponse::success(Merchant::where('parent_merchant_id',$r->attributes->get('merchant')->id)->paginate(50));}
 public function store(Request $r){$p=$r->attributes->get('merchant');$d=$r->validate(['name'=>'required|string|max:255','email'=>'required|email|unique:merchants,email','external_reference'=>'nullable|string|max:100']);$m=Merchant::create(['public_id'=>'mrc_'.Str::ulid(),'parent_merchant_id'=>$p->id,'merchant_type'=>'sub_merchant','name'=>$d['name'],'email'=>$d['email'],'password'=>Hash::make(Str::random(64)),'status'=>'pending','external_reference'=>$d['external_reference']??null]);return ApiResponse::success($m,201);}
}