<?php

namespace App\Controllers;

use App\Models\ContactModel;

class Home extends BaseController
{
    public function index(): string
    {
        // Menyediakan data dinamis produk jika diperlukan di masa mendatang
        $data['products'] = [
            [
                'title' => 'Pagar BRC Hotdip Tipe A',
                'specs' => ['Proses Galvanisasi Celup Panas', 'Tahan Cuaca Ekstrem', 'Cocok Kemiringan: 5-10mm']
            ],
            [
                'title' => 'Pagar BRC Hotdip Tipe B',
                'specs' => ['Struktur Kuat', 'Garansi Anti Karat Tinggi', 'Cocok Kemiringan: 3-5mm']
            ],
            [
                'title' => 'Pintu BRC Hotdip',
                'specs' => ['Struktur Baja', 'Berkualitas Tinggi', 'Cocok Namun: 200mm']
            ]
        ];

        return view('landing_page', $data);
    }

    public function submitContact()
    {
        // Validasi input data dari user
        $rules = [
            'name'    => 'required|min_length[3]|max_length[100]',
            'email'   => 'required|valid_email|max_length[100]',
            'message' => 'required|min_length[10]'
        ];

        if (!$this->validate($rules)) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $contactModel = new ContactModel();
        
        $contactModel->save([
            'name'    => $this->request->getPost('name'),
            'email'   => $this->request->getPost('email'),
            'message' => $this->request->getPost('message')
        ]);

        return redirect()->back()->with('success', 'Pesan Anda berhasil dikirim! Tim kami akan segera menghubungi Anda.');
    } // <-- Batas penutup fungsi submitContact yang benar berada di sini

    // Sekarang fungsi about berdiri sendiri dengan benar di dalam class Home
    public function about(): string
    {
        // Data tim manajemen/operasional pagar BRC
        $data['teams'] = [
            ['name' => 'Budi Santoso', 'role' => 'Direktur Utama', 'img' => 'https://images.unsplash.com/photo-1560250097-0b93528c311a?q=80&w=200'],
            ['name' => 'Hendra Wijaya', 'role' => 'Kepala Produksi Galvanis', 'img' => 'https://images.unsplash.com/photo-1519085360753-af0119f7cbe7?q=80&w=200'],
            ['name' => 'Maya Lestari', 'role' => 'Quality Control Exec', 'img' => 'https://images.unsplash.com/photo-1573496359142-b8d87734a5a2?q=80&w=200'],
        ];

        return view('about_page', $data);
    }
}
