<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Sponsor;

class SponsorController extends Controller
{
    

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'fullName' => 'required|string',
            'gender' => 'required|in:male,female,other',
            'dob' => 'required|date_format:d/m/Y',
            'address' => 'required|string',
            'city' => 'required|string',
            'state' => 'required|string',
            'zip' => 'required|string',
            'country' => 'required|string',
            'phone' => 'required|string',
            'email' => 'required|email',
            'pan' => 'nullable|string',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 400);
        }

        $sponsor = Sponsor::create($validator->validated());

        return response()->json(['message' => 'Sponsor registration successful!', 'sponsor' => $sponsor], 201);
    }

    public function sponsorList()
    {
        $sponsors = Sponsor::all();

        return response()->json(['sponsors' => $sponsors], 200);
    }

   

    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'fullName' => 'required|string|max:255',
            'gender' => 'required|string|in:male,female,other',
            'dob' => 'required|date_format:d/m/Y',
            'address' => 'required|string|max:255',
            'city' => 'required|string|max:255',
            'state' => 'required|string|max:255',
            'zip' => 'required|string|max:255',
            'country' => 'required|string|max:255',
            'phone' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'pan' => 'nullable|string|max:255',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 400);
        }

        $sponsor = Sponsor::findOrFail($id);
        $sponsor->update($validator->validated());

        return response()->json(['message' => 'Sponsor details updated successfully!', 'sponsor' => $sponsor], 200);
    }

    public function destroy($id)
    {
        $sponsor = Sponsor::findOrFail($id);
        $sponsor->delete();

        return response()->json(['message' => 'Sponsor deleted successfully!'], 200);
    }
}
