<?php
 namespace App\Policies; use App\Models\Admin; use App\Models\CommunicationTemplate; use Illuminate\Auth\Access\HandlesAuthorization; class CommunicationTemplatePolicy { use HandlesAuthorization; public function viewAny(Admin $admin): bool { return $admin->hasAnyPermission([ 'view email templates', 'view sms templates', 'view notification templates', ]); } public function view(Admin $admin, CommunicationTemplate $template): bool { return $admin->hasPermissionTo($this->key('view', $template)); } public function create(Admin $admin): bool { return $admin->hasAnyPermission([ 'create email templates', 'create sms templates', 'create notification templates', ]); } public function update(Admin $admin, CommunicationTemplate $template): bool { return $admin->hasPermissionTo($this->key('update', $template)); } public function delete(Admin $admin, CommunicationTemplate $template): bool { if ($template->is_system) { return false; } $action = $template->channel === 'email' ? 'update' : 'delete'; return $admin->hasPermissionTo($this->key($action, $template)); } private function key(string $action, CommunicationTemplate $template): string { return match (true) { $template->channel === 'email' => "{$action} email templates", $template->channel === 'sms' => "{$action} sms templates", default => "{$action} notification templates", }; } } 