<?php
namespace App\Http\Controllers\Company\Masters;
use App\Http\Controllers\Controller;
use App\Models\{AuditLog, OtherName};
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class OtherNamesController extends Controller {
    public function index(Request $request) {
        $this->gate();
        $cid = auth()->user()->company_id;
        $q = OtherName::withoutGlobalScopes()->where('company_id',$cid)->whereNull('deleted_at');
        if ($request->filled('search')) { $s=$request->search; $q->where(fn($x)=>$x->where('name','like',"%$s%")->orWhere('code','like',"%$s%")); }
        if ($request->filled('status')) { $q->where('is_active',$request->status==='active'); }
        $others = $q->orderBy('name')->paginate(25)->withQueryString();
        $stats = ['total'=>OtherName::withoutGlobalScopes()->where('company_id',$cid)->whereNull('deleted_at')->count(),'active'=>OtherName::withoutGlobalScopes()->where('company_id',$cid)->whereNull('deleted_at')->where('is_active',true)->count()];
        return view('masters.other-names.index',compact('others','stats'));
    }
    public function store(Request $request) {
        $this->gate('create');
        $cid = auth()->user()->company_id;
        $v = $request->validate(['code'=>['nullable','string','max:30',Rule::unique('other_names')->where(fn($q)=>$q->where('company_id',$cid)->whereNull('deleted_at'))],'name'=>'required|string|max:150','type'=>'nullable|string|max:50','phone'=>'nullable|string|max:30','email'=>'nullable|email|max:150','address'=>'nullable|string|max:500']);
        $o = OtherName::withoutGlobalScopes()->create(array_merge($v,['company_id'=>$cid,'is_active'=>true]));
        return back()->with('success',"'{$o->name}' added.");
    }
    public function update(Request $request, OtherName $otherName) {
        $this->gate('edit'); $this->sc($otherName);
        $cid = auth()->user()->company_id;
        $v = $request->validate(['code'=>['nullable','string','max:30',Rule::unique('other_names')->where(fn($q)=>$q->where('company_id',$cid)->whereNull('deleted_at'))->ignore($otherName->id)],'name'=>'required|string|max:150','type'=>'nullable|string|max:50','phone'=>'nullable|string|max:30','email'=>'nullable|email|max:150','address'=>'nullable|string|max:500','is_active'=>'boolean']);
        $v['is_active'] = $request->boolean('is_active',true);
        $otherName->update($v);
        return back()->with('success',"'{$otherName->name}' updated.");
    }
    public function destroy(OtherName $otherName) {
        $this->gate('delete'); $this->sc($otherName);
        $guard = app(\App\Services\MasterDeletionGuard::class);
        if ($message = $guard->blockingMessage($otherName)) {
            return back()->withErrors(['error' => $message]);
        }
        $n=$otherName->name; $id=$otherName->id; $guard->hardDelete($otherName);
        AuditLog::record('hard_delete','other_names',(string)$id,null,null,"Other name permanently deleted: {$n}");
        return back()->with('success',"'$n' permanently deleted.");
    }
    private function gate(string $a='view') { if(!auth()->user()->hasPermission('masters',$a==='view'?'view':($a==='create'?'create':($a==='edit'?'edit':'delete')))) abort(403); }
    private function sc(OtherName $o) { if($o->company_id!==auth()->user()->company_id) abort(403); }
}
