<?php
// Controllers/CustomerController.php

require_once __DIR__ . '/../_Core/Validator.php';

class CustomerController
{
    protected $pdo;

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function createOrUpdate($data, $user)
    {
        $validated = $this->validateCustomer($data);

        if ($validated['error']) {
            return ['error' => true, 'msg' => $validated['msg']];
        }

        $payload = $validated['data'];
        $payload['assigned_to'] = $user['id'];
        $payload['team_id'] = $user['team_id'];

        if (!empty($data['id'])) {
            return $this->updateCustomer($data['id'], $payload);
        }

        return $this->createCustomer($payload);
    }

    private function validateCustomer($data)
    {
        $errors = [];

        $name = Validator::str($data['customer_name'] ?? "", 3, 120);
        if (!$name) $errors[] = "Customer name is invalid.";

        $mobile = Validator::mobile($data['mobile'] ?? "");
        if (!$mobile) $errors[] = "Invalid mobile number (10 digits required).";

        $email = Validator::optionalStr($data['email'] ?? "", 120);

        $address = Validator::optionalStr($data['address'] ?? "", 255);

        $status = Validator::str($data['customer_status'] ?? "", 3, 50);

        if ($errors) {
            return ['error' => true, 'msg' => implode(" ", $errors)];
        }

        return [
            'error' => false,
            'data' => [
                'customer_name' => $name,
                'mobile' => $mobile,
                'email' => $email,
                'address' => $address,
                'customer_status' => $status
            ]
        ];
    }

    private function createCustomer($payload)
    {
        $sql = "INSERT INTO sd_customers
                (customer_name, mobile, email, address, customer_status, assigned_to, team_id, created_at)
                VALUES
                (:name, :mobile, :email, :address, :status, :assigned_to, :team_id, NOW())";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([
            ':name' => $payload['customer_name'],
            ':mobile' => $payload['mobile'],
            ':email' => $payload['email'],
            ':address' => $payload['address'],
            ':status' => $payload['customer_status'],
            ':assigned_to' => $payload['assigned_to'],
            ':team_id' => $payload['team_id'],
        ]);

        return [
            'error' => false,
            'msg' => 'Customer created successfully.',
            'id' => $this->pdo->lastInsertId()
        ];
    }

    private function updateCustomer($id, $payload)
    {
        $sql = "UPDATE sd_customers SET
                customer_name = :name,
                mobile = :mobile,
                email = :email,
                address = :address,
                customer_status = :status
                WHERE id = :id";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([
            ':id' => $id,
            ':name' => $payload['customer_name'],
            ':mobile' => $payload['mobile'],
            ':email' => $payload['email'],
            ':address' => $payload['address'],
            ':status' => $payload['customer_status']
        ]);

        return [
            'error' => false,
            'msg' => 'Customer updated successfully.'
        ];
    }
}
